gpt4 book ai didi

url - 如何获取刚刚添加到 PHPhotoLibrary 中的图像的 URL

转载 作者:行者123 更新时间:2023-12-03 14:57:27 27 4
gpt4 key购买 nike

我在两种情况下使用 UIImagePickerController

  • 选择照片库中的现有图像
  • 拍一张新照片

  • 在第一种情况下,当我从库中选择图像时,我可以轻松地在委托(delegate)方法中获取 URL:
    - (void) imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
    {
    // Get the URL
    NSURL *url = [info valueForKey:UIImagePickerControllerReferenceURL];
    ...
    }

    但是当我拍摄一张新照片时,该图像尚未在照片库中,并且还没有 URL。所以,我首先需要在库中添加图像。但是,如何获取新 Assets 的 URL?

    这是我在照片库中添加图像的代码
    - (void) imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
    {
    // Get the image
    UIImage *image = [info objectForKey:UIImagePickerControllerOriginalImage];

    // Add the image in the library
    [[PHPhotoLibrary sharedPhotoLibrary]

    performChanges:^
    {
    // Request creating an asset from the image.
    PHAssetChangeRequest *createAssetRequest = [PHAssetChangeRequest creationRequestForAssetFromImage:image];

    // Get the URL of the new asset here ?
    ...
    }

    completionHandler:^(BOOL success, NSError *error)
    {
    if (!success) { ...; return; }

    // Get the URL of the new asset here ?
    ...
    }
    ];
    }

    最佳答案

    我没有找到获取 URL 的方法,但也许 localIdentifier 可以帮助您完成相同的工作。

    采用

    NSString* localId = [[assetChangeRequest placeholderForCreatedAsset] localIdentifier];

    获取本地ID并稍后获取图像。
    __block NSString* localId;
    // Add it to the photo library
    [[PHPhotoLibrary sharedPhotoLibrary] performChanges:^{
    PHAssetChangeRequest *assetChangeRequest = [PHAssetChangeRequest creationRequestForAssetFromImage:image];

    if (self.assetCollection) {
    PHAssetCollectionChangeRequest *assetCollectionChangeRequest = [PHAssetCollectionChangeRequest changeRequestForAssetCollection:self.assetCollection];
    [assetCollectionChangeRequest addAssets:@[[assetChangeRequest placeholderForCreatedAsset]]];
    }
    localId = [[assetChangeRequest placeholderForCreatedAsset] localIdentifier];
    } completionHandler:^(BOOL success, NSError *error) {
    PHFetchResult* assetResult = [PHAsset fetchAssetsWithLocalIdentifiers:@[localId] options:nil];
    if (!success) {
    NSLog(@"Error creating asset: %@", error);
    } 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) {
    UIImage* newImage = [UIImage imageWithData:imageData];
    self.imageView.image = newImage;
    }];
    }
    }];

    关于url - 如何获取刚刚添加到 PHPhotoLibrary 中的图像的 URL,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30196969/

    27 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com