iphone - iPhone 上图片的文件路径

标签 iphone objective-c ios photo

我有兴趣将文件(图像)从 iPhone 库/相机胶卷上传到远程网络服务器。我已经有一个脚本可以将任何文件从手机上传到网络服务器。但是,我假设要从 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];
}

关于iphone - iPhone 上图片的文件路径,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11569968/

相关文章:

ios - HealthKit:保存在 iPhone 上的锻炼在 Activity App 中不可见

objective-c - respondsToSelector 和 class_getInstanceMethod

iphone - SQLite 问题 : sqlite3_open([databasePath UTF8String], &database) == SQLITE_OK) 不工作

ios - UIAccessibilityVoiceOverStatusChanged 的​​替代品是什么

objective-c - 使用数组传递数据但在 TableView 委托(delegate)中显示为空

iphone - 当可以使用 NSUserDefaults 时,为什么要使用单例在多个 View Controller 之间共享数据?

iphone - 在没有 UITableView 的情况下使用 NSFetchedResultsController

objective-c - 你如何改变 Cocoa 中窗口关闭按钮的状态?

ios - 在 UITableView 上滑动删除以使用 UIPanGestureRecognizer

UITabBarController 和 UINavigation Controller 的 iOS8 旋转问题