我有兴趣将文件(图像)从 iPhone 库/相机胶卷上传到远程 Web 服务器。我已经有一个脚本可以将任何文件从手机上传到网络服务器。但是,我假设要从 iPhone 上传图像,我需要该图像的路径。一旦用户从相机胶卷中选择所述图像,有什么方法可以做到这一点?即,如何获取相机胶卷中所选图像的文件路径?
我试过无济于事。
谢谢!
我有兴趣将文件(图像)从 iPhone 库/相机胶卷上传到远程 Web 服务器。我已经有一个脚本可以将任何文件从手机上传到网络服务器。但是,我假设要从 iPhone 上传图像,我需要该图像的路径。一旦用户从相机胶卷中选择所述图像,有什么方法可以做到这一点?即,如何获取相机胶卷中所选图像的文件路径?
我试过无济于事。
谢谢!
您将需要查看ALAssetsLibrary功能 - 这些功能可让您访问存储在 iOS 设备上的照片和视频库中的照片和视频。
具体来说,类似:
ALAssetsLibrary *assets = [[ALAssetsLibrary alloc] init];
[assets enumerateGroupsWithTypes:ALAssetsGroupSavedPhotos
usingBlock:^(ALAssetsGroup *group, BOOL *stop) {
[group enumerateAssetsUsingBlock:^(ALAsset *asset, NSUInteger index, BOOL *stop) {
//the ALAsset should be one of your photos - stick it in an array and after this runs, use it however you need
}
}
failureBlock:^(NSError *error) {
//something went wrong, you can't access the photo gallery
}
];
编辑
如果您使用的是 UIImagePickerController 而不是纯粹的编程方法,这将大大简化它:
在:
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
UIImage *img = [info objectForKey:UIImagePickerControllerEditedImage];
//you can use UIImagePickerControllerOriginalImage for the original image
//Now, save the image to your apps temp folder,
NSString *path = [NSTemporaryDirectory() stringByAppendingPathComponent:@"upload-image.tmp"];
NSData *imageData = UIImagePNGRepresentation(img);
//you can also use UIImageJPEGRepresentation(img,1); for jpegs
[imageData writeToFile:path atomically:YES];
//now call your method
[someClass uploadMyImageToTheWebFromPath:path];
}