UIImagePickerController よもやま話 http://www.spookies.co.jp 草苅 景 [email_address]
はじめに
UIImagePickerController  とは iPhone  で、 Camera  で写真を撮ったり、 Photo Library  から写真を選択するためのクラス。 非常に簡単に利用できる。 iPhone SDK  が提供している、 Camera  を使うための公式な唯一の手段。
実際の ソースコード
@interface CameraViewController : UIViewController <UIImagePickerControllerDelegate, UINavigationControllerDelegate> { UIImagePickerController* cameraController; } @property (nonatomic, assign) UIImagePickerController* cameraController; @end
#define SOURCETYPE  UIImagePickerControllerSourceTypeCamera (省略) if(![UIImagePickerController isSourceTypeAvailable:SOURCETYPE]){ return; } cameraController = [[[UIImagePickerController alloc] init] retain]; cameraController.sourceType = SOURCETYPE; cameraController.delegate =  self; cameraController.allowsImageEditing = NO; [self.view.superview addSubview:cameraController.view]; //[self presentModalViewController:cameraController animated:YES];
UIImagePickerControllerSourceType
UIImagePickerControllerSourceType UIImagePickerControllerSourceTypeCamera Camera  で撮影。 UIImagePickerControllerSourceTypePhotoLibrary Photo Library  から画像選択。 UIImagePickerControllerSourceTypeSavedPhotosAlbum SavedPhotoAlbum  から画像選択。
Photo Library Saved Photo Album
Camera
UIImagePickerControllerDelegate
-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingImage:(UIImage *)image editingInfo:(NSDictionary *)editingInfo; { [picker release]; //  写真選択時の処理 } - (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker { [picker release]; //  キャンセル時の処理 }
UIImagePickerController  に茶茶を入れる
allowsImageEditing =NO
画像編集しない場合 UINavigationControllerDelegate  を実装。 Camera, PhotoLibrary  共に表示をいじれる。 - (void)navigationController:(UINavigationController *)navigationController willShowViewController:(UIViewController *)viewController animated:(BOOL)animated { //  画面カスタマイズコード }
ToyCamera  っぽい カメラインターフェース を実現
ここを消したい
for (UIView* view in currentView.subviews) { NSLog(@&quot;view is a %@&quot;, [view description]); } ひたすら掘ります
その結果
- (void)navigationController:(UINavigationController *)navigationController willShowViewController:(UIViewController *)viewController animated:(BOOL)animated { UIView* cameraView = [viewController.view.subviews objectAtIndex:0]; UIView* cropOverlay = [cameraView.subviews objectAtIndex:3]; UIView* imgView = [cropOverlay.subviews objectAtIndex:0]; UIView* lcdLayer = [cropOverlay.subviews objectAtIndex:1]; [imgView setHidden: YES]; [lcdLayer setHidden: YES]; }
allowsImageEditing = YES
こんな感じで 表示したい
いろいろやり方を考えた なんとか  allowsImageEditing  画面起動をハンドリングしたい。 方法を発見できず。 PhotoLibrary  全体に  View  を載せて、自前判定。 キャンセルが押されたとき、スクロールされた場合などの処理が複雑。 PhotoLibrary  の  Photo  上に同じ大きさの透明な  View  を載せる。 Photo 1 つずつのオブジェクトまで辿りつけなかった。
最終的に行き着いたやり方
View  の時間監視
(省略) [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(checkView) userInfo:nil repeats:YES]; touchView = [[MyTouchView alloc] init]; touchView.frame = CGRectMake(0, 0, 320, 480); touchView.backgroundColor = [UIColor blueColor]; touchView.opaque = YES; touchView.alpha = 0.5; touchView.cameraviewcontroller = self; [self.view.superview addSubview:touchView]; [self.view.superview sendSubviewToBack:touchView]; }
- (void)checkView { UIView* temp; [touchView.superview sendSubviewToBack:touchView]; if ((temp = [self getViewFromArray:self.view.superview.subviews num:2]) != nil) { if((temp = [self getViewFromArray:temp.subviews num:0]) != nil) { if((temp = [self getViewFromArray:temp.subviews num:1]) != nil) { if(([[temp description] length] > 15) && ([[[temp description] substringToIndex:15] isEqualToString:@&quot;<PLCropOverlay:&quot;])) { [touchView.superview bringSubviewToFront:touchView]; } } } } }
- (UIView *)getViewFromArray:(NSArray*)viewArray num:(int)i { if([viewArray count] <= i) return nil; return [viewArray objectAtIndex:i]; }
最後に
非公開 API を使えばもう少しいろいろできるみたいです。 興味がある方はそちらも調べてみて下さい。
ご静聴ありがとうございました!

UIImagePickerController よもやま話