如何获取UIImage的完整文件路径

时间:2015-05-11 11:15:14

标签: ios objective-c

我有一个UI图像,我想找到它的路径。我怎么想这样做?

UIIImage *image ; // I have saved an image here
NSString *documentsPath = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)[0]; 

NSString *filePath = [documentsPath stringByAppendingPathComponent: ????????? ]; // How to get the image from UIImage ?

1 个答案:

答案 0 :(得分:0)

我告诉你问题的解决方案

  STEP 1: If you have the image in Bundle,just follow the below code

      NSString *stringImagePath = [[NSBundle mainBundle] pathForResource:@"Your_Image_Name" ofType:@"jpg"]; // or ofType:@"png", etc. 

  STEP 2: If you have image in particular place,just do the following code

     NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,
                                                     NSUserDomainMask, YES);
     NSString *documentsDirectory = [paths objectAtIndex:0];
     NSString* path = [documentsDirectory stringByAppendingPathComponent:
                  @"yourimage.png" ]; //or yourimage.jpg
     NSLog(@"The image path is-%@",path);
     NSData* data = UIImagePNGRepresentation(image);
     [data writeToFile:path atomically:YES];