6

次のコードを使用して、カメラからキャプチャしたばかりの画像の名前を取得しようとしています。しかし、[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]);
    }
}
4

3 に答える 3

5

アプリケーション内からカメラでキャプチャした画像には名前がありません。常にゼロです。その画像をプログラムでフォト ギャラリーに保存する必要があり、任意の名前で保存できます。

于 2014-04-16T08:25:12.350 に答える
1

次のコードを didFinishPickingMediaWithInfo に追加します。

NSURL *mediaUrl;
NSString *imageURLString;

 self.selectImage = [info valueForKey:UIImagePickerControllerEditedImage];

if (mediaUrl == nil) {

    if (self.selectImage == nil) {

        self.selectImage =  [info valueForKey:UIImagePickerControllerOriginalImage];
        DebugLog(@"Original image picked.");

    }else {

        DebugLog(@"Edited image picked.");

    }

}

mediaUrl = (NSURL *)[info valueForKey:UIImagePickerControllerMediaURL];
imageURLString=[mediaUrl absoluteString];

DebugLog(@"Hi Image URL STRING : - %@",imageURLString);

if ([StringUtils string:imageURLString contains:@"PNG"] || [StringUtils string:imageURLString contains:@"png"]) {


    self.isJPG = NO;
    self.profileImageName = @"profileImageName.png";

} else if ([StringUtils string:imageURLString contains:@"JPG"] || [StringUtils string:imageURLString contains:@"jpg"]) {


    self.isJPG = YES;
    self.profileImageName = @"profileImageName.jpg";

}

kUTTypeMovie にカメラを設定すると、あなただけが referenceurl と mediaurl を取得します。kUTTypeImage の場合は null を返します。

于 2014-04-16T08:26:19.807 に答える