uiimagepickercontroller - 获取从照片库中选择的图像的名称

Pri*_*a V 6 iphone photolibrary uiimagepickercontroller ipad

我正在尝试将图像从我的iPhone/iPod touch上传到我的在线存储库,我已成功从相册中选择图像,但我遇到一个问题我想知道图像的名称,如image1.jpg或某些东西像那样.我怎么知道所选图像的名称.

auc*_*uco 6

(UIImage*)[info valueForKey:UIImagePickerOriginalImage]您可以使用AssetsLibrary.framework并导出实际的源文件(包括格式,名称和所有元数据),而不是使用通常的图像选择器方法将选定的图像作为UIImage的实例.这还具有保留原始文件格式(png或jpg)的优点.

#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;
    }
}
Run Code Online (Sandbox Code Playgroud)


Rah*_*rma 4

我想知道确切的图像名称不会成为问题,而是为所选图像获取唯一的名称将解决您的目的,以便您可以将图像上传到服务器上并通过其名称跟踪它。也许这可以帮助你

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

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

从选取器中选取图像后,您可以生成一个唯一的名称并将其分配给选取的图像。干杯