【问题标题】:Storing image using NSDocumentDirectory not working?使用 NSDocumentDirectory 存储图像不起作用?
【发布时间】:2011-09-30 09:20:43
【问题描述】:

我正在为 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”不显示照片。我不知道问题出在哪里。

照片还保存了吗? 还是我找回保存照片的方式不对?

【问题讨论】:

    标签: iphone ios objective-c ipad


    【解决方案1】:
    NSString *filePath=[NSString stringWithFormat:@"%@/%@",documentsDir,@"SavedImage.png"];
    imageView2.image=[UIImage imageWithContentsOfFile:filePath];
    

    【讨论】:

      【解决方案2】:

      尝试加载图片:

      [UIImage imageWithContentsOfFile:imageFilePath]
      

      【讨论】:

        猜你喜欢
        • 2018-03-02
        • 1970-01-01
        • 2017-07-17
        • 2019-05-29
        • 2019-12-06
        • 1970-01-01
        • 2011-11-30
        • 1970-01-01
        • 2013-07-14
        相关资源
        最近更新 更多