如何获取iphone库中图像的路径

时间:2012-12-13 07:05:52

标签: iphone objective-c ios path uiimagepickercontroller

我需要从iphone图库上传图片,但我没有获得所选图片的路径(使用图片选择器

在图像选择器委托方法中,我正在执行以下操作

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

}

,信息字典包含以下键值

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

我使用密钥UIImagePickerControllerReferenceURL的值作为将其上传到FTP的图像路径,但我无法上传文件,可能我采取的路径不正确。

任何人都可以帮我解决如何使用图像选择器获取所选图像的实际路径

2 个答案:

答案 0 :(得分:13)

可能对你有帮助

[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];

答案 1 :(得分:1)

转换NSData中的图片,然后上传此NSData

您在NSData中的图片

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

或者您可以使用

NSData转换您的图片
NSData *dataImage = UIImagePNGRepresentation(yourImageView.image);

更多阅读NSData Class Reference