How can i get the name of image picked through pho

2019-01-04 02:01发布

I am picking an image from photo library in iphone application. How will i retrieve the actual image name.

in .h class

UIImageView * imageView;

UIButton * choosePhotoBtn;

in .m class

-(IBAction) getPhoto:(id) sender 
{
    UIImagePickerController * picker = [[UIImagePickerController alloc] init];
    picker.delegate = self;
    if((UIButton *) sender == choosePhotoBtn)
    {
        picker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
    }
    else 
    { 
        picker.sourceType = UIImagePickerControllerSourceTypeCamera;
    }
    [self presentModalViewController:picker animated:YES];
}

- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info 
{
    [picker dismissModalViewControllerAnimated:YES];
    imageView.image = [info objectForKey:@"UIImagePickerControllerOriginalImage"];
}

How will i get the actual name of image ?

I m new in iphone. Please help me.

Thanks in advance.

9条回答
疯言疯语
2楼-- · 2019-01-04 02:08
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any])
{
    let imageUrl          = info[UIImagePickerControllerReferenceURL] as! NSURL
    let imageName         = imageUrl.lastPathComponent
    let documentDirectory = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true).first!
    let photoURL          = NSURL(fileURLWithPath: documentDirectory)
    let localPath         = photoURL.appendingPathComponent(imageName!)
    let image             = info[UIImagePickerControllerOriginalImage]as! UIImage
    let data              = UIImagePNGRepresentation(image)

    do
    {
        try data?.write(to: localPath!, options: Data.WritingOptions.atomic)
    }
    catch
    {
        // Catch exception here and act accordingly
    }

    self.dismiss(animated: true, completion: nil);
}
查看更多
太酷不给撩
3楼-- · 2019-01-04 02:11

Though you may be able to retrieve the last path component and use it like a file name, it is not advisable to do so. These filenames are assigned by the system for iTunes to understand while syncing and are not meant for programmers to access as they could be replaced by some other images in future syncs.

A good round about for this is to assign the current Date as filenames, while saving to images picked from the gallery. You may save it in your documents or library directory and use a mapping PList file to map images to their filename.

Alternatively, you can also assign unique numbers as filenames and access the images using these values.

查看更多
甜甜的少女心
4楼-- · 2019-01-04 02:13

Objective C implementation that works on iOS 10. ALAssetsLibrary seems to be deprecated so you should use PHAsset:

-(void) imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary<NSString *,id> *)info{

    NSURL *imageURL = [info valueForKey:UIImagePickerControllerReferenceURL];
    PHAsset *phAsset = [[PHAsset fetchAssetsWithALAssetURLs:@[imageURL] options:nil] lastObject];
    NSString *imageName = [phAsset valueForKey:@"filename"];

    UIImage *photo = [info valueForKey:UIImagePickerControllerOriginalImage];

    NSLog(@"Picked image: %@ width: %f x height: %f",imageName, photo.size.width, photo.size.height);

    [picker dismissViewControllerAnimated:YES completion:nil];
}
查看更多
闹够了就滚
5楼-- · 2019-01-04 02:16

Simple Swift implementation:

func imagePickerController(picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : AnyObject]) {

    if let referenceUrl = info[UIImagePickerControllerReferenceURL] as? NSURL {

        ALAssetsLibrary().assetForURL(referenceUrl, resultBlock: { asset in

            let fileName = asset.defaultRepresentation().filename()
            //do whatever with your file name

            }, failureBlock: nil)
        }
    }
}

Remember about: import AssetsLibrary

查看更多
叼着烟拽天下
6楼-- · 2019-01-04 02:16

As of Swift 3 and iOS8+ the .filename is not accessible any more. It is still available through self.valueForKey("filename"), but not quite legal though.

However, I found this answer in the question "iOS8 Photos Framework: How to get the name(or filename) of a PHAsset?" to be short, simple, and legal.

查看更多
女痞
7楼-- · 2019-01-04 02:17

If you are building for iOS 9+ target, you will see a bunch of deprecation warnings with ALAssetsLibrary, i.e.:

'assetForURL(_:resultBlock:failureBlock:)' was deprecated in iOS 9.0: Use fetchAssetsWithLocalIdentifiers:options: on PHAsset to fetch assets by local identifier (or to lookup PHAssets by a previously known ALAssetPropertyAssetURL use fetchAssetsWithALAssetURLs:options:) from the Photos framework instead

As the warning describes, you should use PHAsset. Using swift 2.x, for example, you will need to add import Photos to your file first. Then, in the didFinishPickingMediaWithInfo UIImagePickerControllerDelegate method use fetchAssetsWithALAssetURLs to get the filename:

if let imageURL = info[UIImagePickerControllerReferenceURL] as? NSURL {
    let result = PHAsset.fetchAssetsWithALAssetURLs([imageURL], options: nil)
    let filename = result.firstObject?.filename ?? ""
}

This will set filename to be something like, "IMG_0007.JPG".

查看更多
登录 后发表回答