簡體   English   中英

uiimagepickercontroller - 獲取從照片庫中選擇的圖像的名稱

[英]uiimagepickercontroller - get the name of the image selected from photo library

我正在嘗試將圖像從我的 iPhone/iPod touch 上傳到我的在線存儲庫,我已經成功地從相冊中選擇了圖像,但我遇到了一個問題,我想知道圖像的名稱,例如 image1.jpg 或其他東西像那樣。 我怎么知道所選圖像的名稱。

您可以使用(UIImage*)[info valueForKey:UIImagePickerOriginalImage] AssetsLibrary.framework導出實際的源文件(包括格式、名稱和所有元數據)。 這還具有保留原始文件格式(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;
    }
}

我想知道確切的圖像名稱不是問題,而是為挑選的圖像獲取一個唯一的名稱將解決您的目的,以便您可以將圖像上傳到服務器上並通過其名稱進行跟蹤。 也許這可以幫助你

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

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

從 Picker 中選擇圖像后,您可以生成一個唯一名稱並將其分配給 Picked 圖像。 干杯

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM