Search code examples
iosobjective-calassetslibrary

There is any possibility to get the photo from library using filename?


When ever i am selecting the Image ,i am getting image name using assets library .

This is my below code :

-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary<NSString *,id> *)info
{
    UIImage *img=info[UIImagePickerControllerOriginalImage];
    NSURL *refURL = [info valueForKey:UIImagePickerControllerReferenceURL];

    // define the block to call when we get the asset based on the url (below)
    ALAssetsLibraryAssetForURLResultBlock resultblock = ^(ALAsset *imageAsset)
    {
        NSString *filename;
        ALAssetRepresentation *imageRep = [imageAsset defaultRepresentation];

        NSLog(@"[imageRep filename] : %@", [imageRep filename]);
        filename=[imageRep filename];

    };

    // get the asset library and fetch the asset based on the ref url (pass in block above)
    ALAssetsLibrary* assetslibrary = [[ALAssetsLibrary alloc] init];
    [assetslibrary assetForURL:refURL resultBlock:resultblock failureBlock:nil];
    link=info[UIImagePickerControllerReferenceURL];

    [picker dismissViewControllerAnimated:YES completion:nil];


}

Inside filename i have the chosen photo name like filename = "IMG_0001.JPG";

Now my question is using the file name can i get the photo from library.

Friends suggest me a way ,tutorial ,etc ...

I have searched but can't get finally i am came here.


Solution

  • No, not from the name alone. The full asset URL can be used to retrieve the photo (assuming it still exists on the device) but there is no API to search for an asset based on name.