简体   繁体   English

如何从Iphone中的指定图像路径中检索图像

[英]How to retrieve image from the specified image path in Iphone

Hi how can i retrieve image from specified image path in iphone. 您好我如何从iPhone中的指定图像路径检索图像。 In my code is 在我的代码中

- (void)imagePickerController:(UIImagePickerController *)UIPicker didFinishPickingImage:(UIImage *)info editingInfo:(NSDictionary *)dictionary
{
    imageView.image=info;
    [UIPicker dismissModalViewControllerAnimated:YES];
    NSLog(@"UImage path= %@",imageView.image);
    NSData* imageData = UIImagePNGRepresentation(imageView.image);
    NSString* imageName = @"MyImage.png";
    NSArray* paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString* documentsDirectory = [paths objectAtIndex:0];
    NSString *fullPathToFile = [documentsDirectory stringByAppendingPathComponent:imageName];
    [imageData writeToFile:fullPathToFile atomically:NO];
    NSLog(@"Image path = %@",fullPathToFile);
}

When i select image, the image should be placed, it will successfully placed to uiimage view. 当我选择图像时,应该放置图像,它将成功放置到uiimage视图中。 And i got the image path. 我得到了图像路径。

When i execute the application in console it showed 当我在控制台中执行应用程序时,它显示

Image path=document = /Users/rr-mac/Library/Application Support/iPhone Simulator/4.3/Applications/46C716D0-13E9-4DF2-BC46-F1071DFCE14C/Documents/MyImage.png

My image is stored in this specified path. 我的图像存储在此指定的路径中。 Here how can i retrieve this image and placed to imageview. 在这里我如何检索此图像并放置到imageview。 Anybody help me to give the code. 有人帮我提供代码。

Thanks in Advance. 提前致谢。

Answer is in your question itself. 答案就在你的问题中。

  NSArray* paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, 
                                      NSUserDomainMask, YES);
  NSString* documentsDirectory = [paths objectAtIndex:0];

  fullPathToFile = [documentsDirectory stringByAppendingPathComponent:imageName];

   imageView.image =  [UIImage imageNamed:fullPathToFile];

To retrieve the UIImage in directory path, you should use : 要在目录路径中检索UIImage,您应该使用:

UIImage *image = [UIImage imageWithContentsOfFile:fullPathToFile];

Full Code : 完整代码:

NSString *imageName = @"MyImage.png";
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *fullPathToFile = [documentsDirectory stringByAppendingPathComponent:imageName];
UIImage *image = [UIImage imageWithContentsOfFile:fullPathToFile];
self.imageView.image = image;

如果您的图像位于应用程序的文档文件夹下,则可以使用以下代码,

yourImageView.image = [UIImage imageNamed:@"MyImage.png"];

To your comment in the previous answer, I am adding this 对于您在上一个答案中的评论,我添加了这个

Get the doc directory by 获取doc目录

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *docdirpath = [paths objectAtIndex:0];

Append the image path to this docdirpath and then use it in imageNamed: method in UIImage. 将图像路径附加到此docdirpath,然后在UIImage中的imageNamed:方法中使用它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM