How to get the URL of an image just added in PHPhotoLibrary

后端 未结 6 976
臣服心动
臣服心动 2020-12-30 03:11

I am using the UIImagePickerController in two cases

  • to select an existing image in the Photo Library
  • to take a new picture

In the first

6条回答
  •  暗喜
    暗喜 (楼主)
    2020-12-30 04:02

    Retrieve image URL:

    - (void)processImage:(UIImage*)image type:(NSString*)mimeType forCallbackId:(NSString*)callbackId
        {
            __block NSString* localId;
    
            // Add it to the photo library
            [[PHPhotoLibrary sharedPhotoLibrary] performChanges:^{
                PHAssetChangeRequest *assetChangeRequest = [PHAssetChangeRequest creationRequestForAssetFromImage:image];
    
                localId = [[assetChangeRequest placeholderForCreatedAsset] localIdentifier];
            } completionHandler:^(BOOL success, NSError *err) {
                if (!success) {
                    NSLog(@"Error saving image: %@", [err localizedDescription]);
                } else {
                    PHFetchResult* assetResult = [PHAsset fetchAssetsWithLocalIdentifiers:@[localId] options:nil];
                    PHAsset *asset = [assetResult firstObject];
                    [[PHImageManager defaultManager] requestImageDataForAsset:asset
                                                                      options:nil
                                                                resultHandler:^(NSData *imageData, NSString *dataUTI, UIImageOrientation orientation, NSDictionary *info) {
                        NSURL *fileUrl = [info objectForKey:@"PHImageFileURLKey"];
                        if (fileUrl) {
                            NSLog(@"Image path: %@", [fileUrl relativePath]);
                        } else {
                            NSLog(@"Error retrieving image filePath, heres whats available: %@", info);
                        }
                    }];
                }
            }];
        }
    

提交回复
热议问题