4

我需要从 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 的图像路径,但我无法上传文件,可能我采用的路径不正确。

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

4

2 回答 2

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];
于 2012-12-13T07:48:29.813 回答
1

转换你的图像NSData,然后上传这个NSData

您在 NSData 中的图像

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

或者您可以将 yourImage 转换为NSData使用

NSData *dataImage = UIImagePNGRepresentation(yourImageView.image);

有关更多信息,请阅读NSData 类参考

于 2012-12-13T07:15:13.393 回答