2012-03-03 93 views
18

我想問一個問題,或許已經被問過100萬次了。從照片庫中選擇多張圖片

我正在爲iPad製作應用程序,並希望讓用戶能夠從照片庫中多選圖像。我已經有一個工作代碼供用戶一次選擇一個圖像。 (不是我需要的)

我已經下載並查看了ELC圖像選擇器示例代碼,但該代碼與iOS 5或Xcode 4不兼容。即它具有ARC並編譯問題左右,其使用版本和dealloc到處都是。

我只是感到沮喪,蘋果還沒有爲我們的開發人員在大多數iPhone/iPad應用程序中爲這個最常見的功能創建一個內置的API。 (不是一個,但多選圖片)

是否有任何其他示例代碼可用?相信我,我一直在Google上搜索。

+5

將其轉換爲ARC並不困難.. – 2012-03-03 04:44:19

回答

21

好吧,我有這個想通了。資產庫的問題在於它爲您提供了圖像的所有GEO數據。這意味着你的用戶使用你的應用程序的意思是,他們會得到一個提示,說你的應用程序正試圖訪問他們的位置。事實上,你所要做的就是讓他們從他們的相冊中選擇多張圖片。大多數用戶會被認爲是盜版問題。最好的方法是使用imagePickerController的蘋果api。我知道它可以讓你一次選擇一張照片,但如果你添加下面的代碼,它會讓你選擇多張照片。

我這樣做的方式是讓用戶不斷選擇他們想要的圖片,將這些文件保存在應用程序文檔目錄中,直到他們點擊完成按鈕。看到這裏我的示例代碼,並希望這將節省您要去的痛苦通過資產庫

-(IBAction)selectExitingPicture 
{ 
    //Specially for fing iPAD 
    UIImagePickerController *imagePicker = [[UIImagePickerController alloc] init]; 
    imagePicker.delegate = self; 
    imagePicker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary; 
    imagePicker.mediaTypes = [NSArray arrayWithObject:(NSString *)kUTTypeImage]; 

    popoverController = [[UIPopoverController alloc] initWithContentViewController:imagePicker]; 
    [popoverController presentPopoverFromRect:CGRectMake(0.0, 0.0, 400.0, 300.0) 
          inView:self.view 
      permittedArrowDirections:UIPopoverArrowDirectionAny 
          animated:YES]; 
} 

//Done button on top 
- (void)navigationController:(UINavigationController *)navigationController 
     willShowViewController:(UIViewController *)viewController 
        animated:(BOOL)animated 
{  
    //NSLog(@"Inside navigationController ..."); 


    if (!doneButton) 
    { 
     doneButton = [[UIBarButtonItem alloc] initWithTitle:@"Done" 
                 style:UIBarButtonItemStyleDone 
                target:self action:@selector(saveImagesDone:)]; 
    } 

    viewController.navigationItem.rightBarButtonItem = doneButton; 
} 

- (IBAction)saveImagesDone:(id)sender 
{ 
    //NSLog(@"saveImagesDone ..."); 

    [popoverController dismissPopoverAnimated:YES]; 
} 


-(void)imagePickerController:(UIImagePickerController *)picker 
     didFinishPickingImage : (UIImage *)image 
       editingInfo:(NSDictionary *)editingInfo 
{ 


    //DONT DISMISS 
    //[picker dismissModalViewControllerAnimated:YES]; 
    //[popoverController dismissPopoverAnimated:YES]; 

     IMAGE_COUNTER = IMAGE_COUNTER + 1; 

     imageView.image = image; 

     // Get the data for the image 
     NSData* imageData = UIImageJPEGRepresentation(image, 1.0); 


     // Give a name to the file 
     NSString* incrementedImgStr = [NSString stringWithFormat: @"UserCustomPotraitPic%d.jpg", IMAGE_COUNTER]; 


     NSArray* paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); 
     NSString* documentsDirectory = [paths objectAtIndex:0]; 

     // Now we get the full path to the file 
     NSString* fullPathToFile2 = [documentsDirectory stringByAppendingPathComponent:incrementedImgStr]; 

     // and then we write it out 
     [imageData writeToFile:fullPathToFile2 atomically:NO]; 

} 

//現在使用此代碼來獲取用戶選擇的照片。無論你想在你的代碼

NSArray* paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask ,YES); 
     NSString* documentsPath = [paths objectAtIndex:0]; 
     NSString* dataFile = [documentsPath stringByAppendingPathComponent:@"UserCustomPotraitPic1.jpg"]; 

     NSData *potraitImgData = [NSData dataWithContentsOfFile:dataFile]; 
     backgroundImagePotrait = [UIImage imageWithData:potraitImgData]; 
+0

嗨...不是從照片庫中選擇圖片,我想從私人應用程序目錄中選擇圖片。我的問題是ELCImage選取器控制器是用於從照片庫中選擇多個圖像還是可以爲私人應用程序目錄完成? – 2012-12-27 06:34:59

+2

感謝使用我的代碼,我能夠自定義uiimagepickerview控制器。現在我想指示用戶,一張照片已經從imagepicker選擇圖像右上角的小圖像如何做到這一點。任何幫助.. – srividya 2013-08-05 13:21:04

+0

你好,你能幫我更新UIImagePickerController中選定圖像的設計? – Iya 2016-03-27 10:36:13

9

Apple爲此提供了api。它被稱爲ALAssetsLibrary

使用此功能,您可以選擇在iOS設備上使用照片應用程序進行的多個圖像/視頻和其他操作。

正如documentation蘋果說:

資源庫框架

iOS 4.0引入,該資源庫框架 (AssetsLibrary.framework)爲 獲取照片和視頻基於查詢的接口來自用戶的設備。使用此 框架,您可以訪問照片應用程序通常由 管理的相同資產,包括用戶保存的照片 專輯中的項目以及導入到設備上的任何照片和視頻。 您還可以將新照片和視頻保存回用戶保存的 相冊。

這裏有幾個鏈接,你可以瞭解更多。現在使用它可以搜索ALAssetsLibrary。

Assets Library Reference

http://www.fiveminutes.eu/accessing-photo-library-using-assets-library-framework-on-iphone/

1

稱它爲我用ALAssetsLibrary並推出自己的UI。 UIImagePickerController的問題在於它說你應該關閉didFinishPickingMediaWithInfo回調中的視圖控制器,所以通過不關閉來破壞多個選擇可能會遇到問題。當我第一次嘗試時,我知道我做過。我不記得究竟是哪裏出了問題,但有些情況下UIImagePickerController只是停止工作,如果我沒有像文檔說的那樣解僱它的話。

相關問題