UIImagePickerController で画像を取り込む

setAllowsEditing:YES で取得してるのはググればたくさんあったけど NO で取得してるのはあまり見かけなかったので記録

-(void)changeImage:(UILongPressGestureRecognizer *)sender{

  // インタフェース使用可能なら

  if ( [UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypePhotoLibrary] ) {

    // UIImageControllerの初期化

    UIImagePickerController *ipc = [[UIImagePickerController alloc] init];

    [ipc setSourceType:UIImagePickerControllerSourceTypePhotoLibrary];

    // Delegateセット

    [ipc setDelegate:self];

    // NOに設定。yesでトリミングして画像を切り取る(その範囲の設定がめんどくさそう

    [ipc setAllowsEditing:NO];

    // 指定したViewを一番上に表示する

    [self presentViewController: ipc animated:YES completion:nil];

  }

}

 

//イメージ取得後に呼び出される関数

- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {

  [picker dismissViewControllerAnimated:YES completion:nil];

  // 選択したイメージをimageにセットする

  UIImage *image = [info objectForKey:UIImagePickerControllerOriginalImage];

  // imageをImageViewerにセットする

  NSUserDefaults *ud = [NSUserDefaults standardUserDefaults];

    //画像縮小比率固定で fit

    _chara.contentMode = UIViewContentModeScaleAspectFit;

    [self.chara setImage:image];

    NSData *data = UIImagePNGRepresentation(image);

    NSString *path = [NSString stringWithFormat:@”%@/test.png”,[NSHomeDirectory() stringByAppendingPathComponent:@”Documents”]];

    if ([data writeToFile:path atomically:YES]) {

      NSLog(@”save OK”);

    } else {

      NSLog(@”save NG”);

    }

  [self dismissViewControllerAnimated:YES completion:NULL];

}

UIImagePickerControllerOriginalImage で受け取れば問題なく画像を取得できる