How do I get the original filename of the image selected by UIImagePickerController?

I have implemented a delegate method and can access the selected UIImage like:

- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {
    UIImage *pickedImage = [info valueForKey:UIImagePickerControllerOriginalImage];

      

I want to store it in a directory, but I need the filename. So instead of just choosing an arbitrary filename, it makes sense to choose the same filename as the image.

Is there a way to extract it from this information dictionary?

+2


source to share


2 answers


You can use the UIImagePickerControllerReferenceURL

information dictionary value instead UIImagePickerControllerOriginalImage

. This should indicate the URL of the original media element.



This will give you back an object NSURL

that you can use to create your new filename.

+3


source


  • Use the ALA Assets library infrastructure
  • Use below code

    
        ALAssetsLibrary *library = [[ALAssetsLibrary alloc] init];
        UIImage *image = [info objectForKey:UIImagePickerControllerEditedImage];
        [library writeImageToSavedPhotosAlbum:image.CGImage orientation:(ALAssetOrientation)image.imageOrientation completionBlock:^(NSURL *assetURL, NSError *error )
        {
           [library assetForURL:assetURL resultBlock:^(ALAsset *asset )
           {
             ALAssetsLibraryAssetForURLResultBlock resultblock = ^(ALAsset *imageAsset)
             {
                 ALAssetRepresentation *imageRep = [imageAsset defaultRepresentation];
                 NSLog(@"reference image filename picking from camera: %@", [imageRep filename]);
             };
             ALAssetsLibrary* assetslibrary = [[ALAssetsLibrary alloc] init];
             [assetslibrary assetForURL:assetURL resultBlock:resultblock failureBlock:nil];
           }
           failureBlock:^(NSError *error )
           {
               NSLog(@"Error loading asset");
           }];
        }];
    
    
          

    imageView.image = [info objectForKey:UIImagePickerControllerEditedImage];
    imageView.contentMode = UIViewContentModeScaleAspectFit;
    
          



    >



0


source







All Articles