2017-01-21 3 views
0

사진에서 자른 이미지를 얻으려고합니다. 지금은 이미지의 원래 해상도를 변경하기 때문에사진 이미지에서 얼굴 이미지 선택 (자막)

- (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

@ 항공기 예, 있습니다. –

+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은 얼굴의 위치를 ​​얻기 위해 원하는 자리를 확인하기 쉽지 않습니다. RSKImageCropper가 편리한 선택 일 수 있습니다.