2012-07-19 71 views
0

我有兴趣从iPhone库/照相机卷上传文件(图像)到远程Web服务器。我已经有了一个脚本,可以将任何文件从手机上传到网络服务器。但是,我认为要从iPhone上传图像,我需要PATH来表示图像。一旦用户从相机胶卷中选取所述图像,是否有任何方法可以完成?也就是说,如何获取在相机胶卷中选择的图像的文件路径?iPhone上的图像的文件路径

我试图无果。

谢谢!

回答

6

您将需要查看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,而不是纯粹的programatical方法,这大大简化了它:

在:

- (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]; 
} 
+0

有没有办法t o没有这个库吗?我需要访问任何给定的照片X,只是他们从选取器点击的照片。 – Objc55 2012-07-19 22:15:27

+0

出于某种原因,我以为你试图做纯粹的编程。如果您使用的是UIImagePickerController,那么它变得非常容易。我会编辑我的答案来反映这一点。 – CrimsonDiego 2012-07-19 22:48:06

+0

@CrimsonDiego我用你的code.But我怎么能检查特定的照片,并得到它的路径。 – Muju 2017-07-13 11:11:00