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

折月煮酒 提交于 2019-12-03 07:52:42

问题


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.


回答1:


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




回答2:


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;
    }
}


来源:https://stackoverflow.com/questions/5864814/uiimagepickercontroller-get-the-name-of-the-image-selected-from-photo-library

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!