我有两个,我的看法。 1. imageview - img 2. Uibutton -takepic 该应用程序的目的是在ImageView上显示图像。 当我点击按钮时,我有两个选项。或者拍摄相册,或拍照相机。 当我从相册中拍摄照片时,图像会保存在我的应用程序中并列在imageview上,当我关闭将在后台运行并返回应用程序的应用程序时,图像仍然存在。 问题是相机,当相机在imageview上列出并保存在设备的相册中时我会拍照。 但当我关闭将在后台运行的应用程序并返回应用程序时,图像不会驻留在imageview上为什么?
这是我的完整代码:
-(void)viewDidLoad {
[super viewDidLoad];
NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
NSURL *selectedImageURL = [NSURL URLWithString:[userDefaults objectForKey:@"selectedImageURL"]];
if (selectedImageURL)
{
[self getImageFromAssetsAtURL:selectedImageURL completion:^(UIImage *image, NSError *error)
{
if (!error && image)
{
self.img.image = image;
}
}];
}
// Creating a Circular Profile Image.
self.img.layer.cornerRadius = self.img.frame.size.width / 2.0;
self.img.clipsToBounds = YES;
// Adding Border to image.
self.img.layer.borderWidth = 6.0;
self.img.layer.borderColor = [UIColor blackColor].CGColor;
}
-(void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
-(IBAction)takePic:(id)sender {
// ALERT SHEET.
UIAlertController *alert = [UIAlertController alertControllerWithTitle:nil message:nil preferredStyle:UIAlertControllerStyleActionSheet];
//CAMERA
UIAlertAction *openCamrea = [UIAlertAction actionWithTitle:@"צלם" style:UIAlertActionStyleDefault handler:^(UIAlertAction * action)
{
// If device has no camera.
if (![UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera])
{
UIAlertController *alertNoCamera = [UIAlertController alertControllerWithTitle:@"Error" message:@"Device has no camera" preferredStyle:UIAlertControllerStyleAlert];
UIAlertAction *ok = [UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleDefault handler:^(UIAlertAction * action){}];
[alertNoCamera addAction:ok];
[self presentViewController:alertNoCamera animated:YES completion:nil];
}
else// if have a camera.
{
UIImagePickerController *picker = [[UIImagePickerController alloc] init];
picker.delegate = self;
picker.allowsEditing = YES;
picker.sourceType = UIImagePickerControllerSourceTypeCamera;
[self presentViewController:picker animated:YES completion:NULL];
}
}];
// GALLERY
UIAlertAction *openGallery = [UIAlertAction actionWithTitle:@"גלריה" style:UIAlertActionStyleDefault handler:^(UIAlertAction * action)
{
UIImagePickerController *picker = [[UIImagePickerController alloc] init];
picker.delegate = self;
picker.allowsEditing = YES;
picker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
[self presentViewController:picker animated:YES completion:NULL];
}];
[alert addAction:openCamrea];
[alert addAction:openGallery];
[self presentViewController:alert animated:YES completion:nil];
}
-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary<NSString *,id> *)info
{
UIImage *image = info[UIImagePickerControllerOriginalImage];
NSURL *selectedImageURL = info[UIImagePickerControllerReferenceURL];
if(picker.sourceType == UIImagePickerControllerSourceTypeCamera)
{
UIImageWriteToSavedPhotosAlbum(image, self, @selector(image:didFinishSavingWithError:contextInfo:), nil);
// set image
self.img.image = image;
}
else{
self.img.image = image;
}
// save image url
NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
[userDefaults setObject:selectedImageURL.absoluteString forKey:@"selectedImageURL"];
[userDefaults synchronize];
// set image
self.img.image = image;
// dismiss
[self dismissViewControllerAnimated:YES completion:nil];
}
-(void)imagePickerControllerDidCancel:(UIImagePickerController *)picker {
[picker dismissViewControllerAnimated:YES completion:NULL];
}
// chkeing if image save on album ot not.
- (void)image:(UIImage *)image didFinishSavingWithError:(NSError *)error contextInfo:(void *)contextInfo
{
UIAlertAction *ok = [UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleDefault handler:^(UIAlertAction * action){}];
if (error) // Unable to save the image
{
UIAlertController *alertErorr = [UIAlertController alertControllerWithTitle:@"Erorr" message:@"Unable to save image to Photo Album." preferredStyle:UIAlertControllerStyleAlert];
[alertErorr addAction:ok];
[self presentViewController:alertErorr animated:YES completion:nil];
}
else // All is well
{
UIAlertController *alertSuccess = [UIAlertController alertControllerWithTitle:@"Success" message:@"Image saved to Photo Album." preferredStyle:UIAlertControllerStyleAlert];
[alertSuccess addAction:ok];
self.img.image=image;
[self presentViewController:alertSuccess animated:YES completion:nil];
}
[self dismissViewControllerAnimated:YES completion:NULL];
}
-(void)getImageFromAssetsAtURL:(NSURL *)imageURL completion:(void (^) (UIImage *, NSError *))completion
{
PHFetchResult *result = [PHAsset fetchAssetsWithALAssetURLs:@[imageURL] options:nil];
if (result.count == 1) {
PHAsset *asset = result.firstObject;
[[PHImageManager defaultManager] requestImageDataForAsset:asset options:nil resultHandler:^(NSData * _Nullable imageData, NSString * _Nullable dataUTI, UIImageOrientation orientation, NSDictionary * _Nullable info) {
UIImage *selectedImage = [UIImage imageWithData:imageData];
completion(selectedImage, nil);}];
} else {
completion(nil, nil);
}
}
@end
答案 0 :(得分:2)
最佳方法是将图片保存到您的应用沙箱(Document Directory
)。通常我不建议在NSUserDefaults
中保存图像,因为NSUserDefaults
应该用来存储小设置而不是原始图像数据。
准备好图像后,将其保存在应用程序文档文件夹中。您可以稍后从保存的路径中删除图像!
您可以将图像路径存储在NSUserDefaults
但不是图像!!!
保存图片:
将此代码放入didFinishPickingMediaWithInfo
方法:
如果图片位于UIImageJPEGRepresentation
,请使用.jpeg扩展名,如果图片位于UIImagePNGRepresentation
使用.png扩展名
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *filePath = [NSString stringWithFormat:@"%@/%@", documentsDirectory, @"myImage.png"];
NSLog(@"filePath %@", filePath);
NSError *error;
[image writeToFile:filePath atomically:NO];
要取回图片:
NSArray *arrayPaths =
NSSearchPathForDirectoriesInDomains(
NSDocumentDirectory,
NSUserDomainMask,
YES);
NSString *path = [arrayPaths objectAtIndex:0];
NSString* fileName = [path stringByAppendingPathComponent:@"myImage.png"];
UIImage *image=[UIImage imageWithContentsOfFile:fileName];
答案 1 :(得分:0)
库图片是保存的图片,但是当您从相机为您的应用程序拍照时,这将不会保存在任何地方...所以当应用程序进入后台时它将被删除....首先将图像保存在任何地方.. ..快速方法将它保存到conteudo
,如下所示:
NSUserDefaults
此后,当你来到前景,然后在你viewWillAppear方法你可以像这样设置你的图像
[NSUserdefaults standardUserDefaults] setObject:YourImage forkey:"myImage"];
多数人......好运
答案 2 :(得分:0)
试试这个..
选择&#34; Strong&#34; &#34; UIImage&#34;的属性说&#34; myImage&#34;然后为其分配从相机捕获的图像。
现在使用&#34; myImage&#34;将图像分配给imageView时。