简体   繁体   English

如何获取图像的路径,从图库中选择或通过相机捕获的图像

[英]How to get path for image, selected from gallery or captured through camera

I am trying to get path for image, selected from gallery or captured through camera. 我试图获取图像的路径,从图库中选择或通过相机捕获的图像。 I have tried using following code 我尝试使用以下代码

imageView.image= [info objectForKey:@"UIImagePickerControllerOriginalImage"];
NSData *webData = UIImagePNGRepresentation(imageView.image);
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *localFilePath = [documentsDirectory stringByAppendingPathComponent:png];
[webData writeToFile:localFilePath atomically:YES];
NSLog(@"localFilePath.%@",localFilePath);

but when i check through terminal "documents " folder found to be empty. 但是当我通过终端“ documents”文件夹检查时发现是空的。

Use this code 使用此代码

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *savedImagePath = [documentsDirectory stringByAppendingPathComponent:@"savedImage.png"];

imageView.image= [info objectForKey:@"UIImagePickerControllerOriginalImage"];
NSData *webData = UIImagePNGRepresentation(imageView.image);
[imageData writeToFile:savedImagePath atomically:NO];  

NSLog(@"localFilePath.%@",localFilePath);

If you still get it empty then try by checking if NSData is created properly 如果仍然为空,请尝试检查是否正确创建了NSData。

NSLog(@"webData.%@",webData);

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

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