簡體   English   中英

從照片庫中選擇多個圖像

[英]Select Multiple Images from Photo Library

我要問一個可能已經被問過一百萬次的問題。

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

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

我只是很沮喪,蘋果還沒有為我們的開發人員創建一個內置的 api,用於我們大多數 iPhone/ipad 應用程序中最常請求的功能。 (不是一張而是多選圖片)

有沒有其他可用的示例代碼? 相信我,我已經在谷歌上搜索了一段時間。

好的,我已經弄清楚了。 Assets Library 的問題在於它為您提供了圖像的所有 GEO 數據。 對於使用您的應用程序的用戶來說,這意味着他們會收到一條提示,說明您的應用程序正在嘗試訪問他們的位置。 事實上,您要做的就是讓他們從他們的相冊中選擇多個圖像。 大多數用戶會認為這是盜版問題而被關閉。 最好的方法是使用imagePickerController 的apples api。 我知道它可以讓您一次選擇一張圖片,但是如果您添加以下代碼,它將讓您選擇多張圖片。

我的做法是讓用戶不斷選擇他們想要的圖片,不斷將這些文件保存在應用程序文檔目錄中,直到他們點擊完成按鈕。 在這里查看我的示例代碼,希望它可以為您省去通過 Assets Library 的痛苦

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

蘋果為此提供了api。 它被稱為ALAssetsLibrary

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

正如 Apple 在文檔中所說:

資產庫框架

在 iOS 4.0 中引入的資產庫框架 (AssetsLibrary.framework) 提供了一個基於查詢的界面,用於從用戶設備檢索照片和視頻。 使用此框架,您可以訪問通常由照片應用程序管理的相同資產,包括用戶保存的相冊中的項目以及導入設備的任何照片和視頻。 您還可以將新照片和視頻保存回用戶已保存的相冊。

這里有幾個鏈接,您可以在其中了解更多信息。 現在要使用它,您可以搜索 ALAssetsLibrary。

資產庫參考

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

從 iOS 14 開始,新的照片選擇器現在支持多張圖片選擇。 這是來自 Apple 的示例代碼: 在 iOS 中選擇照片和視頻

我使用ALAssetsLibrary並推出了我自己的 UI。 UIImagePickerController的問題在於它說您應該在didFinishPickingMediaWithInfo回調中關閉視圖控制器,因此通過不關閉來破解多個選擇可能會遇到問題。 我知道我第一次嘗試時做到了。 我不記得到底出了什么問題,但有些情況下UIImagePickerController如果我沒有像文檔所說的那樣關閉它,就會停止工作。

暫無
暫無

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

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