gpt4 book ai didi

iphone - 使用 NSDocumentDirectory 存储图像不起作用?

转载 作者:行者123 更新时间:2023-11-29 04:57:50 26 4
gpt4 key购买 nike

我正在为 iPad 开发此应用程序。我的第一个屏幕将显示 2 个 UIImageView(ImageView、ImageView2)和 2 个 UIButton(确认、浏览)。

我将简要描述我的应用程序的用途。点击“浏览”按钮后,它将显示 iPad 照片库中的照片。选择照片后,照片库将关闭,“ImageView”将显示所选照片。当点击“确认”按钮时,它将使用 NSDocumentDirectory 将图像存储到我的应用程序的项目中。之后,它将检索保存的照片并将其显示在“ImageView2”上。

浏览按钮..

- (IBAction) browsePhoto:(id)sender
{
UIImagePickerController *imagePickerController = [[UIImagePickerController alloc] init];
imagePickerController.delegate = self;
imagePickerController.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
UIPopoverController *popover = [[UIPopoverController alloc] initWithContentViewController:imagePickerController];
[popover setPopoverContentSize:CGSizeMake(320,320)];
[popover presentPopoverFromRect:CGRectMake(200,200,-100,-100) inView:self.view permittedArrowDirections:UIPopoverArrowDirectionAny animated:YES];
self.popoverController = popover;
[imagePickerController release];
}

在 ImageView 上显示选定的图像..

- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingImage:(UIImage *)selectedImage editingInfo:(NSDictionary *)editingInfo 
{
[self.popoverController dismissPopoverAnimated:YES];
imageView.image = selectedImage;
}

确认按钮..

- (IBAction) confirmPhoto:(id)sender
{

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDir = [paths objectAtIndex:0];
NSString *savedImagePath = [documentsDir stringByAppendingPathComponent:@"SavedImage.png"];
UIImage *image = imageView.image;
NSData *imageData = UIImagePNGRepresentation(image);
[imageData writeToFile:savedImagePath atomically:NO];
imageView2.image = [UIImage imageNamed:@"SavedImage.png"];
}

我可以从照片库中查看照片,选择一张照片,并使“ImageView”显示所选照片。但是当我点击“确认”按钮时,“ImageView2”不显示照片。我不知道问题出在哪里。

照片保存了吗?或者我以前检索保存的照片的方式是错误的?

最佳答案

NSString *filePath=[NSString stringWithFormat:@"%@/%@",documentsDir,@"SavedImage.png"];
imageView2.image=[UIImage imageWithContentsOfFile:filePath];

关于iphone - 使用 NSDocumentDirectory 存储图像不起作用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7608345/

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