iOSでフォトライブラリから画像を取得する方法


iOSでフォトライブラリから画像を取得する場合は、
UIImagePickerControllerで sourceTypeをUIImagePickerControllerSourceTypePhotoLibraryで指定すれば取得できる。
ただし、この場合は、１枚しか取得できない。

## UIImagePickerController

クラスはUIImagePickerControllerDelegateのプロトコルを使う。

````test.m
- (IBAction)touchDownButton1:(id)sender
{
    if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypePhotoLibrary]) {
        UIImagePickerController *imagePickerController = [[UIImagePickerController alloc] init];
        [imagePickerController setSourceType:UIImagePickerControllerSourceTypePhotoLibrary];
        [imagePickerController setAllowsEditing:YES];
        [imagePickerController setDelegate:self];
        
        [self presentViewController:imagePickerController animated:YES completion:nil];
    }
}


- (IBAction)touchDownButton2:(id)sender
{
    if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeSavedPhotosAlbum]) {
        UIImagePickerController *imagePickerController = [[UIImagePickerController alloc] init];
        [imagePickerController setSourceType:UIImagePickerControllerSourceTypeSavedPhotosAlbum];
        [imagePickerController setAllowsEditing:YES];
        [imagePickerController setDelegate:self];
        
        [self presentViewController:imagePickerController animated:YES completion:nil];
    }
}

- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
    //画像が選択されたとき。オリジナル画像をUIImageViewに突っ込む
    UIImage *origImage = (UIImage *)[info objectForKey:UIImagePickerControllerOriginalImage];
    if (origImage) {
        [self.previewImage setImage:origImage];
    }
    
    [self dismissViewControllerAnimated:YES completion:nil];
}

- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker
{
    //cancelのとき。なにもしないで閉じる
    [self dismissViewControllerAnimated:YES completion:nil];
}

````

ちなみに、iPadの場合、UIPopoverControllerを使わなければ行けないとのことですが、シミュレータでは使わなくても落ちませんでした。
実機じゃないと再現しないのかな？


# 複数枚取得の場合

複数枚選択するには、以下のようなライブラリを使う必要がある。

ELCImagePickerController
https://github.com/B-Sides/ELCImagePickerController

QBImagePickerController 
https://github.com/questbeat/QBImagePickerController


## ELCImagePickerController

ボタンを用意して、そこから画像を取得するサンプル
クラスはELCImagePickerControllerDelegateプロトコルを使う

````test.m

//ボタンを押したら画像選択させる
- (IBAction)touchDownButton3:(id)sender {
    ELCAlbumPickerController *albumController = [[ELCAlbumPickerController alloc] init];
    ELCImagePickerController *elcPickerController = [[ELCImagePickerController alloc] initWithRootViewController:albumController];
    [albumController setParent:elcPickerController];
    [elcPickerController setImagePickerDelegate:self];
    
    [self presentViewController:elcPickerController animated:YES completion:nil];
}

//きちんと選んだとき
- (void)elcImagePickerController:(ELCImagePickerController *)picker didFinishPickingMediaWithInfo:(NSArray *)info
{
    [self dismissViewControllerAnimated:YES completion:nil];
    if ([info count] < 1) {
        return;
    }
    
    UIImage *image;
    for (NSMutableDictionary *item in info) {
        image = [item objectForKey:UIImagePickerControllerOriginalImage];
    }
    // とりあえず最後の画像だけ表示
    [self.previewImage setImage:image];
    
}

//キャンセルしたとき
- (void)elcImagePickerControllerDidCancel:(ELCImagePickerController *)picker
{
    [self dismissViewControllerAnimated:YES completion:nil];
}

````

![imagepicker1.png](https://qiita-image-store.s3.amazonaws.com/0/4044/b7c8856a-12ad-ba04-e188-67e94a1c571f.png "imagepicker1.png")


## QBImagePickerController

ELCImagePickerとちょっと使い方違う。

````test.m
- (IBAction)touchDownButton4:(id)sender {
    QBImagePickerController *qbImagePickerController = [[QBImagePickerController alloc]init];
    [qbImagePickerController setDelegate:self];
    [qbImagePickerController setAllowsMultipleSelection:YES];
    

	//NavigationControllerを用意して上げる必要あり。
    UINavigationController *navigationController = [[UINavigationController alloc] initWithRootViewController:qbImagePickerController];
    
    [self presentViewController:navigationController animated:YES completion:nil];
}

- (void)imagePickerController:(QBImagePickerController *)imagePickerController didSelectAsset:(ALAsset *)asset
{
    //setAllowsMultipleSelectionがNOのとき
    ALAssetRepresentation *representation = [asset defaultRepresentation];
    UIImage *image = [UIImage imageWithCGImage:[representation fullScreenImage]];
    [self.imageView setImage:image];
    
    [self dismissViewControllerAnimated:YES completion:nil];
}

- (void)imagePickerController:(QBImagePickerController *)imagePickerController didSelectAssets:(NSArray *)assets
{
    //setAllowsMultipleSelectionがYESのとき
    ALAssetRepresentation *representation;
    for (ALAsset *asset in assets) {
        representation = [asset defaultRepresentation];
    }
    UIImage *image = [UIImage imageWithCGImage:[representation fullScreenImage]];
    [self.imageView setImage:image];
    
    [self dismissViewControllerAnimated:YES completion:nil];
}

- (void)imagePickerControllerDidCancel:(QBImagePickerController *)imagePickerController
{
    [self dismissViewControllerAnimated:YES completion:nil];
}

````

![imagepicker2.png](https://qiita-image-store.s3.amazonaws.com/0/4044/639121dc-4595-314c-8c00-c4b82f86194a.png "imagepicker2.png")