I am trying to get the name of the image I just shot from the camera using the following code. But [info objectForKey:@"UIImagePickerControllerReferenceURL"] always returns zero. How can I get the 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]) {
UPDATE:
A bit late, but here is how I get the name of the image or video:
- Check
UIImagePickerControllerMediaURL if it is null media - this is an image if it is not a video. - If the image or video is just recorded or recorded, save it in the photo album
- Use
ALAssetsLibrary to request a file name.
Here is the code for saving and retrieving media:
- (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]); } }
The actual method that gets the media name:
- (void)getMediaName:(UIImage*)originalImage url:(NSURL*)url { @try { ALAssetsLibraryAssetForURLResultBlock resultblock = ^(ALAsset *asset) { if (asset == nil) return; ALAssetRepresentation *assetRep = [asset defaultRepresentation]; NSString *fileName = [assetRep filename];
source share