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

时间:2011-10-18 10:09:05

标签: iphone

您如何从iphone中的指定图像路径中检索图像。我的代码是

- (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);
}

当我选择图像时,应该放置图像,它将成功放置到uiimage视图中。我得到了图像路径。

当我在控制台中执行应用程序时,显示

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

我的图片存储在此指定的路径中。在这里我如何检索此图像并放置到imageview。有人帮我提供代码。

先谢谢。

4 个答案:

答案 0 :(得分:1)

答案就在你的问题中。

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

  fullPathToFile = [documentsDirectory stringByAppendingPathComponent:imageName];

   imageView.image =  [UIImage imageNamed:fullPathToFile];

答案 1 :(得分:1)

要在目录路径中检索UIImage,您应该使用:

UIImage *image = [UIImage imageWithContentsOfFile:fullPathToFile];

完整代码:

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;

答案 2 :(得分:0)

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

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

答案 3 :(得分:0)

对于您在上一个回答中的评论,我正在添加此

通过

获取doc目录
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *docdirpath = [paths objectAtIndex:0];

将图像路径附加到此docdirpath,然后在UIImage中的imageNamed:方法中使用它。