2017-01-21 11 views
0

写真から切り取った画像を取得しようとしています。今のところ、それは画像の元の解像度を変更するため写真画像から顔画像を選択して切り抜く(ios)

- (UIImage *)scaleAndRotateImage:(CIFeature *)ciFeature image:(UIImage *)image { 
    static int kMaxResolution = 640; 

    CGImageRef imgRef = image.CGImage; 
    CGFloat width = CGImageGetWidth(imgRef); 
    CGFloat height = CGImageGetHeight(imgRef); 

    CGAffineTransform transform = CGAffineTransformIdentity; 
    CGRect bounds = CGRectMake(0, 0, width, height); 
    if (width > kMaxResolution || height > kMaxResolution) { 
     CGFloat ratio = width/height; 
     if (ratio > 1) { 
      bounds.size.width = kMaxResolution; 
      bounds.size.height = bounds.size.width/ratio; 
     } else { 
      bounds.size.height = kMaxResolution; 
      bounds.size.width = bounds.size.height * ratio; 
     } 
    } 

    CGFloat scaleRatio = bounds.size.width/width; 

    UIImageOrientation orient = image.imageOrientation; 
    switch(orient) { 
     case UIImageOrientationUp: 
      transform = CGAffineTransformIdentity; 
      break; 
       default: 
      [NSException raise:NSInternalInconsistencyException 
         format:@"Invalid image orientation"]; 
    } 

    UIGraphicsBeginImageContext(bounds.size); 
    CGContextRef context = UIGraphicsGetCurrentContext(); 
    if (orient == UIImageOrientationRight || orient == UIImageOrientationLeft) { 
     CGContextScaleCTM(context, -scaleRatio, scaleRatio); 
     CGContextTranslateCTM(context, -height, 0); 
    } else { 
     CGContextScaleCTM(context, scaleRatio, -scaleRatio); 
     CGContextTranslateCTM(context, 0, -height); 
    } 
    CGContextConcatCTM(context, transform); 
    CGContextDrawImage(UIGraphicsGetCurrentContext(), CGRectMake(0, 0, width, height), imgRef); 
    UIImage *returnImage = UIGraphicsGetImageFromCurrentImageContext(); 
    UIGraphicsEndImageContext(); 

    return returnImage; 
} 

しかし、どのように私は(ボックス内の顔画像など)の画像から顔だけを切り抜くことができます:私は、iOSからもらったクロップ画像が検出SDKに直面していることを、次のコードを持っています?

+0

@Aniton Kashpor、正方形の画像に切り抜くことを意味しますか? – aircraft

+0

@ aircraftはい、そうです。 –

+0

特殊な位置またはデフォルトの四角形を切り取りたいですか? – aircraft

答えて

0

あなたがより便利に、よりユーザ定義を使用して画像をトリミングしたい場合は、私がRSKImageCropperを使用することをお勧めいたします、そのgithubの場所はここにある:https://github.com/ruslanskorb/RSKImageCropper

あなたは顔の位置をトリミングしたいので、そうシステムのデフォルトの方法は、あなたがここから学ぶことができ、あなたの条件を満たしていません:https://developer.apple.com/library/content/documentation/AudioVideo/Conceptual/CameraAndPhotoLib_TopicsForIOS/Articles/PickinganItemfromthePhotoLibrary.html#//apple_ref/doc/uid/TP40010408-SW1

コアグラフィックのクロップ画像方法:CGImageCreateWithImageInRectは、あなたのためにあなたがすべき顔の位置を得るために、あなたが望むRECTを確認することは容易ではありません多くの作業を行うと、RSKImageCropperが便利な選択肢になるかもしれません。

関連する問題