4

I am using image picker to pick image from gallery in iPhone device. I have to save those images in the same name that is in the gallery. I don want to name it different after picking image. I tried printing the info dictionary after picking image. Its gives image,url and path and type.. but how to get the actual name of the image any idea. Following is what i get in didFinish

- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info 
{
NSLog(@"Selected image info: %@",info);

}

this prints

Selected image info : (
        {
        UIImagePickerControllerMediaType = "public.image";
        UIImagePickerControllerOriginalImage = "<UIImage: 0x617d390>";
        UIImagePickerControllerReferenceURL = "assets-library://asset/asset.JPG?id=1000000089&ext=JPG";
        }
)

my question is any way to get the actual name from the above.

4

3 Answers 3

5

Using the AssetsLibrary framework, you can get the referenceURL from the info dictionary and fetch the asset. Something like this:

NSURL *assetURL = [info objectForKey:UIImagePickerControllerReferenceURL];

__block NSString *fileName = nil;

ALAssetsLibrary *library = [[[ALAssetsLibrary alloc] init] autorelease];
[library assetForURL:assetURL resultBlock:^(ALAsset *asset)  {
    fileName = asset.defaultRepresentation.fileName;
} failureBlock:nil];
4
  • i am not sure if this gives the name of the image and more over ALAssetsLibrary is Available in iOS 4.0 and later only. Mine is a lesser version of iOS :(. Any ways thanks for your information.
    – rashii
    Jun 29, 2012 at 11:39
  • Ok, you didn't mention that in your question and the answer you accepted is wrong, files in the gallery do have names.
    – hwaxxer
    Jun 30, 2012 at 9:29
  • oh but what kind of names, how to know the structure of those any idea?
    – rashii
    Jul 4, 2012 at 4:49
  • 1
    IMG_0001.JPG, IMG_0002.JPG etc
    – hwaxxer
    Jul 4, 2012 at 8:34
4

Images from picked from the gallery, DO NOT have 'names'. You never assign a name to a picture just captured from the camera and even when you add images through iTunes, the original filenames are stripped off and replaced with filenames that iTunes understands for syncing.

These filenames 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.

Though you mentioned you do not want to assign new names, unfortunately, that's the only way to go about it.

1
  • thanks buddy. The issue is that i dont want to pick the images that has been bicked before. So if not the name then must be the id i guess to use a comparison parameter.
    – rashii
    Jun 29, 2012 at 7:26
0

I don't think they have a name. What I did used on my Application, was to save them with a NSDate (current date) as a name.

1
  • cool bro.. even i have done with saving it in different name. The issue is the second time when I pic image I have compare if it is already been saved or not.
    – rashii
    Jun 29, 2012 at 11:42

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.