Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

uiimagepickercontroller - get the name of the image selected from photo library

I am trying to upload the image from my iPhone/iPod touch to my online repository, I have successfully picked the image from Photo Album but i am facing one problem i want to know the name of the image such as image1.jpg or some thing like that. How i would know the name of the picked image.

like image 958
Priyanka V Avatar asked May 03 '11 03:05

Priyanka V


2 Answers

Instead of using the usual image picker method (UIImage*)[info valueForKey:UIImagePickerOriginalImage] which gives you the selected image as an instance of UIImage, you can use the AssetsLibrary.framework and export the actual source file (including format, name and all metadata). This also has the advantage of the original file format (png or jpg) being preserved.

#import <AssetsLibrary/AssetsLibrary.h>

- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {
    [self dismissPicker];
    // try to get media resource (in case of a video)
    NSURL *resourceURL = [info objectForKey:UIImagePickerControllerMediaURL];
    if(resourceURL) {
        // it's a video: handle import
        [self doSomethingWith:resourceURL];
    } else {
        // it's a photo
        resourceURL = [info objectForKey:UIImagePickerControllerReferenceURL];

        ALAssetsLibrary *assetLibrary = [ALAssetsLibrary new];
        [assetLibrary assetForURL:resourceURL
                      resultBlock:^(ALAsset *asset) {
                          // get data
                          ALAssetRepresentation *assetRep = [asset defaultRepresentation];
                          CGImageRef cgImg = [assetRep fullResolutionImage];
                          NSString *filename = [assetRep filename];
                          UIImage *img = [UIImage imageWithCGImage:cgImg];
                          NSData *data = UIImagePNGRepresentation(img);
                          NSString *cacheDir = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) lastObject];
                          NSURL *tempFileURL = [NSURL fileURLWithPath:[cacheDir stringByAppendingPathComponent:filename]];
                          BOOL result = [data writeToFile:tempFileURL.path atomically:YES];
                          if(result) {
                              // handle import
                              [self doSomethingWith:resourceURL];
                              // remove temp file
                              result = [[NSFileManager defaultManager] removeItemAtURL:tempFileURL error:nil];
                              if(!result) { NSLog(@"Error removing temp file %@", tempFileURL); }
                          }
                      }
                     failureBlock:^(NSError *error) {
                         NSLog(@"%@", error);
                     }];
        return;
    }
}
like image 126
auco Avatar answered Nov 10 '22 16:11

auco


I guess knowing the exact image name would not be an issue rather getting a unique name for the picked image would solve your purpose so that you can upload the image on server and track it via its name. May be this can help you

NSMutableString *imageName = [[[NSMutableString alloc] initWithCapacity:0] autorelease];

CFUUIDRef theUUID = CFUUIDCreate(kCFAllocatorDefault);
if (theUUID) {
    [imageName appendString:NSMakeCollectable(CFUUIDCreateString(kCFAllocatorDefault, theUUID))];
            CFRelease(theUUID);
}
[imageName appendString:@".png"];

After you pick the image from Picker you can generate a unique name and assign it to the Picked image. Cheers

like image 39
Rahul Sharma Avatar answered Nov 10 '22 14:11

Rahul Sharma