gpt4 book ai didi

ios - 关闭应用程序后,从相机拍摄的照片不会出现在 ImageView 中

转载 作者:行者123 更新时间:2023-11-28 21:36:30 26 4
gpt4 key购买 nike

我有两个,我的看法。1. ImageView -img2. Uibutton-拍照该应用程序的目的是在 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

最佳答案

最好的方法是将图像保存到您的应用程序沙箱(文档目录)。通常我不建议在 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];

关于ios - 关闭应用程序后,从相机拍摄的照片不会出现在 ImageView 中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33733999/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com