2011-05-03 51 views
6

我試圖將圖像從我的iPhone/iPod touch的上傳到我的在線資源庫,我已經成功地從相冊選擇了圖像的名字,但我現在面臨一個問題,我想知道圖像的名稱,如image1.jpg或類似的東西。我如何知道所選圖像的名稱。的UIImagePickerController - 得到的照片庫中選擇圖像

+0

你是如何挑選圖片的?你可能實際上已經給出了一個文件路徑嗎? – 2011-05-03 06:42:34

回答

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"]; 

從拾取器中選擇圖像後,您可以生成一個唯一的名稱並將其分配給Picked圖像。 乾杯

+0

我認爲它不可能從照片庫中獲取圖像的確切名稱。 – 2011-05-24 05:31:32

+0

我如何在ARC模式下使用您的代碼? – Rushabh 2013-02-19 06:50:43

6

而是使用通常的圖像拾取方法(UIImage*)[info valueForKey:UIImagePickerOriginalImage],讓你選擇的圖像作爲UIImage的的一個實例的,則可以使用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; 
    } 
} 
+0

什麼是創造時,你已經擁有你需要的所有數據的臨時文件的目的是什麼? – 2014-03-11 19:11:11

+0

確實不是這段代碼所必需的。它在這裏結束了,因爲我通常喜歡通過一個相當小的URL不是巨大的物體,以避免內存壓力可能會崩潰的舊設備。 – auco 2014-03-11 22:59:47

相關問題