ios - UIImagePickerControllerReferenceURL 总是返回 nill

标签 ios uiimagepickercontroller nsurl file-reference-url

我正在尝试使用以下代码获取我刚刚从相机捕获的图像的名称。但是 [info objectForKey:@"UIImagePickerControllerReferenceURL"] 总是返回 nil。如何获取 URL?

- (void)imagePickerController:(UIImagePickerController *)picker
didFinishPickingMediaWithInfo:(NSDictionary *)info {
    self.myinfo = info;
    NSLog(@"Dismissing camera ui...");
    [self.cameraUI dismissViewControllerAnimated:YES completion:nil];

    NSLog(@"Getting media url...");
    NSString *mediaURL = [info objectForKey:UIImagePickerControllerMediaURL];
    NSLog(@"Media url = %@", mediaURL);

    NSLog(@"Getting media type...");
    NSString *mediaType = [info objectForKey:UIImagePickerControllerMediaType];
    NSLog(@"Selected mediaType: %@", mediaType);

    if(mediaURL) {
        NSLog(@"This is a video = %@", mediaURL);

        if (![mediaType isEqualToString:(NSString*)kUTTypeVideo]) {
            UISaveVideoAtPathToSavedPhotosAlbum(mediaURL, self, @selector(video:didFinishSavingWithError:contextInfo:), NULL);
        }
    } else {
        NSLog(@"This is a photo...");
        self.originalImage = (UIImage *) [info objectForKey:UIImagePickerControllerOriginalImage];

        if (self.source == UIImagePickerControllerSourceTypeCamera && [mediaType isEqualToString:(NSString*)kUTTypeImage]) {
            // Image captured from camera
            NSLog(@"Saving new image...");

            if (self.source != UIImagePickerControllerSourceTypePhotoLibrary) {
                UIImageWriteToSavedPhotosAlbum(self.originalImage, self,
                    @selector(image:didFinishSavingWithError:usingContextInfo:), nil);
            }
        }
        // Image selected from previous images.
        else {
            NSLog(@"Getting reference url...");
            self.referenceURL = [info objectForKey:@"UIImagePickerControllerReferenceURL"];
            NSLog(@"Reference url = %@", [self.referenceURL absoluteString]);

            [self saveAssetData:self.originalImage :info];
        }
    }
}

- (void)image:(UIImage *)image
didFinishSavingWithError:(NSError *)error
 usingContextInfo:(void*)ctxInfo {

    if (error) {
        NSLog(@"Resim kaydedilemedi: %@", [error localizedDescription]);
        NSString *title = @"Resim kaydedilemedi!";
        NSString* message = @"Resim kaydedilirken hata oluştu!";
        [self alertStatus:message:title];
    } else {
        NSLog(@"Save asset data...");
        [self saveAssetData:image :self.myinfo];
    }
}

- (void)saveAssetData:(UIImage*)originalImage :(NSDictionary*)info {
    self.assetLibrary = [[ALAssetsLibrary alloc] init];
    NSURL *url = [info objectForKey:@"UIImagePickerControllerReferenceURL"];

    ALAssetsLibraryAssetForURLResultBlock resultblock = ^(ALAsset *asset)
    {
        ALAssetRepresentation *assetRep = [asset defaultRepresentation];

        NSString *filename = [assetRep filename];
        NSLog(@"File name = %@", filename);

        if(self.selectedMediaNames == nil)
            self.selectedMediaNames = [[NSMutableArray alloc] init];

        [self.selectedMediaNames addObject:filename];
        [self.tableView reloadData];
        [self.activitIndicator stopAnimating];
        [self.activitIndicator setHidden:true];

        HMXSharedDataManager *sharedDataManager =
        [HMXSharedDataManager sharedManager];

        [sharedDataManager.uploaMedias addObject:originalImage];
        [sharedDataManager.uploaMediaNames addObject:filename];
    };

    ALAssetsLibraryAccessFailureBlock failureblock  = ^(NSError *error)
    {
        NSLog(@"%@", error);
    };

    [self.assetLibrary assetForURL:url resultBlock:resultblock failureBlock:failureblock];
}

更新:

有点晚了,但这里是我如何获取图像或视频的名称:

  • 检查 UIImagePickerControllerMediaURL,如果它是 null 媒体是一个图像,如果不是它是一个视频
  • 如果图片或视频刚刚拍摄或录制,请将其保存到相册
  • 使用ALAssetsLibrary查询文件名。

这是保存和获取媒体的代码:

- (void)imagePickerController:(UIImagePickerController *)picker
didFinishPickingMediaWithInfo:(NSDictionary *)info {
    @try {
        [self.cameraUI dismissViewControllerAnimated:YES completion:nil];
        mediaURL = [info objectForKey:UIImagePickerControllerMediaURL];

        // If mediaURL is not null this should be a video
        if(mediaURL) {

            // This video is new just recorded with camera
            if (self.source == UIImagePickerControllerSourceTypeCamera) {
                // First save the video to photos album
                ALAssetsLibrary *library = [ALAssetsLibrary new];
                [library writeVideoAtPathToSavedPhotosAlbum:mediaURL completionBlock:^(NSURL *assetURL, NSError *error){
                    if (error) {
                        DDLogDebug(@"Failed to save the photo to photos album...");
                    } else {
                        // Get the name of the video
                        [self getMediaName:nil url:assetURL];
                    }
                }];
            } else { // This is a video that recorded before
                // Get the name of the video
                [self getMediaName:nil url:[info objectForKey:UIImagePickerControllerReferenceURL]];
            }
        }
        // This is an image
        else {
            self.originalImage = (UIImage*)[info objectForKey:UIImagePickerControllerOriginalImage];

            // This image is new just taken with camera
            if (self.source == UIImagePickerControllerSourceTypeCamera) {
                // First save the image to photos album
                ALAssetsLibrary *library = [ALAssetsLibrary new];
                [library writeImageToSavedPhotosAlbum:[self.originalImage CGImage]
                                          orientation:(ALAssetOrientation)[self.originalImage imageOrientation]
                                      completionBlock:^(NSURL *assetURL, NSError *error){
                    if (error) {
                        DDLogDebug(@"Failed to save the vide to photos album...");
                    } else {
                        // Get the name of the image
                        [self getMediaName:self.originalImage url:assetURL];
                    }
                }];
            } else { // This is an image that taken before
                // Get the name of the image
                [self getMediaName:self.originalImage
                                url:[info objectForKey:@"UIImagePickerControllerReferenceURL"]];
            }
        }
    }
    @catch (NSException *exception) {
        DDLogError(@"%@", [exception description]);
    }
}

获取媒体名称的实际方法:

- (void)getMediaName:(UIImage*)originalImage url:(NSURL*)url {
    @try {
        ALAssetsLibraryAssetForURLResultBlock resultblock = ^(ALAsset *asset) {
            if (asset == nil) return;
            ALAssetRepresentation *assetRep = [asset defaultRepresentation];
            NSString *fileName = [assetRep filename];
            // Do what you need with the file name here
        };

        ALAssetsLibraryAccessFailureBlock failureblock  = ^(NSError *error) {
            DDLogError(@"Failed to get image or video name : %@", error);
        };

        ALAssetsLibrary *library = [ALAssetsLibrary new];
        [library assetForURL:url resultBlock:resultblock failureBlock:failureblock];
    }
    @catch (NSException *exception) {
        DDLogError(@"%@", [exception description]);
    }
}

最佳答案

您在应用程序中使用相机拍摄的图像没有名称。它总是零。您必须以编程方式将该图像保存在照片库中,并且可以使用您想要的任何名称进行保存。

关于ios - UIImagePickerControllerReferenceURL 总是返回 nill,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23103826/

相关文章:

ios - 向下或向上滑动 MKMapView 指定数量的像素

ios - imagePickerController 移动和缩放不起作用

iphone - 使用 à è ò ù 字符进行 stringWithContentsOfURL 编码时出错

ios - SWIFT - xcode 6 beta 6 中的 KVO .. observeValueForKeyPath 不再被调用

ios - 如何将 html 实体转换为 nsstring?

ios - Xcode 7 自动布局是否损坏或难以使用

ios - UIImagePickercontroller 不使用按钮不保存

ios - 上传到AWS服务器上传失败?

ios - 如何从 URL 获取图像并放入 NSArray?

objective-c - CGImageSourceCreateWithURL 总是返回 NULL