2013-07-18 19 views
15

私はInstagramのようなカメラを作成しようとしています。ここでユーザーはボックスを見ることができ、画像はそのボックスに切り取られます。何らかの理由で、カメラは画面の一番下までは行かず、最後の近くで切り取られます。私はまた、その広場の中にイメージが320x320になるようにイメージを切り取るにはどうしたらよいでしょうか?iOSカスタムUIImagePickerControllerカメラの四角形への切り取り

enter image description here

答えて

38

はここ(UIImagePickerControllerを再実装せずに)それを行うための最も簡単な方法です。まず、オーバーレイを使用して、カメラフィールドを正方形にします。あなたが戻ってUIImagePickerControllerから画像を取得した後、このような何かの正方形にそれをトリミングし、

UIImagePickerController *imagePickerController = [[UIImagePickerController alloc] init]; 
imagePickerController.sourceType = source; 

if (source == UIImagePickerControllerSourceTypeCamera) { 
    //Create camera overlay 
    CGRect f = imagePickerController.view.bounds; 
    f.size.height -= imagePickerController.navigationBar.bounds.size.height; 
    CGFloat barHeight = (f.size.height - f.size.width)/2; 
    UIGraphicsBeginImageContext(f.size); 
    [[UIColor colorWithWhite:0 alpha:.5] set]; 
    UIRectFillUsingBlendMode(CGRectMake(0, 0, f.size.width, barHeight), kCGBlendModeNormal); 
    UIRectFillUsingBlendMode(CGRectMake(0, f.size.height - barHeight, f.size.width, barHeight), kCGBlendModeNormal); 
    UIImage *overlayImage = UIGraphicsGetImageFromCurrentImageContext(); 
    UIGraphicsEndImageContext(); 

    UIImageView *overlayIV = [[UIImageView alloc] initWithFrame:f]; 
    overlayIV.image = overlayImage; 
    [imagePickerController.cameraOverlayView addSubview:overlayIV]; 
} 

imagePickerController.delegate = self; 
[self presentViewController:imagePickerController animated:YES completion:nil]; 

その後:ここでの例では、3.5" 画面(あなたがiPhone 5のために働くためにそれを更新する必要があるだろう)のためです:。

//Crop the image to a square 
CGSize imageSize = image.size; 
CGFloat width = imageSize.width; 
CGFloat height = imageSize.height; 
if (width != height) { 
    CGFloat newDimension = MIN(width, height); 
    CGFloat widthOffset = (width - newDimension)/2; 
    CGFloat heightOffset = (height - newDimension)/2; 
    UIGraphicsBeginImageContextWithOptions(CGSizeMake(newDimension, newDimension), NO, 0.); 
    [image drawAtPoint:CGPointMake(-widthOffset, -heightOffset) 
        blendMode:kCGBlendModeCopy 
         alpha:1.]; 
    image = UIGraphicsGetImageFromCurrentImageContext(); 
    UIGraphicsEndImageContext(); 
} 

そして、あなたが行われている

+0

ありがとうございます。とにかく、カメラオープニングアニメーションをオーバーレイの上に置くことができるかどうか疑問に思っていましたか? – NSDavidObject

+0

残念ながら、公に文書化されたAPIを使用して(つまり、Appleがアプリケーションを拒否するようなことをしないで)、これを行う方法はありません。これを正しく実行したければ、 'UIImagePickerController'を再実装する必要があります。 – Ander

+1

iphone 5と以前のiphoneの両方で動作させるには、どうすればよいですか? –

4

@Anders答えはiPhone 5で私を修正するために非常に近かった私は、iPhone 5用にハードコードオーバーレイを追加するには、以下の修正をした:

CGRect f = imagePickerController.view.bounds; 
f.size.height -= imagePickerController.navigationBar.bounds.size.height; 
UIGraphicsBeginImageContext(f.size); 
[[UIColor colorWithWhite:0 alpha:.5] set]; 
UIRectFillUsingBlendMode(CGRectMake(0, 0, f.size.width, 124.0), kCGBlendModeNormal); 
UIRectFillUsingBlendMode(CGRectMake(0, 444, f.size.width, 52), kCGBlendModeNormal); 
UIImage *overlayImage = UIGraphicsGetImageFromCurrentImageContext(); 
UIGraphicsEndImageContext(); 

UIImageView *overlayIV = [[UIImageView alloc] initWithFrame:f]; 
overlayIV.image = overlayImage; 
overlayIV.alpha = 0.7f; 
[imagePickerController setCameraOverlayView:overlayIV];` 

私はこれが誰かを助けることを願っています。

+6

IOS7のiPhone 5での私の経験は素晴らしかったです。具体的には、オーバーレイを見ることができましたが、ネイティブのズームコントロールを使用できませんでした。最後に、写真を撮って "Retake"と "Use Photo"ボタンが現れたら、いずれのボタンもクリックできませんでした。 YMMV –

関連する問題