how to get the path of the image which is in iphon

2020-06-06 05:08发布

i need to upload a image from iphone gallery ,but i am not getting the path of the picked image (using image picker)

in the image picker delegate method i am doing the following

- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {
    [picker dismissModalViewControllerAnimated:YES];
    imageView.image = [info objectForKey:@"UIImagePickerControllerOriginalImage"];

    NSString *strImagePath = [info valueForKey:UIImagePickerControllerReferenceURL];
    NSString *strImagePath1 = [info valueForKey:UIImagePickerControllerMediaURL];
    NSLog(@"%@",strImagePath);
    NSLog(@"%@",strImagePath1);
    NSLog(@"%@",info);

}

and the info dictionary contains following key values

{
    UIImagePickerControllerMediaType = "public.image";
    UIImagePickerControllerOriginalImage = "<UIImage: 0x16adc0>";
    UIImagePickerControllerReferenceURL = "assets-library://asset/asset.PNG?id=1000000153&ext=PNG";
}

i used the value for key UIImagePickerControllerReferenceURL as the image path to upload it to FTP but i couldnt upload the file, probably the path which i taking is incorrect.

can anybody help me out how can i take the actual path of the picked image using image picker

2条回答
小情绪 Triste *
2楼-- · 2020-06-06 05:55

May be it will helpful for u

[picker dismissModalViewControllerAnimated:YES];
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);

UIImage *image = [UIImage imageWithContentsOfFile:localFilePath];
查看更多
看我几分像从前
3楼-- · 2020-06-06 06:02

Convert your image in NSData and then Upload this NSData.

Your image in NSData

NSData *dataImage = UIImageJPEGRepresentation([info objectForKey:@"UIImagePickerControllerOriginalImage"],1);

Or you can convert yourImage in NSData using

NSData *dataImage = UIImagePNGRepresentation(yourImageView.image);

For more read NSData Class Reference

查看更多
登录 后发表回答