문제

UIImagePickerController를 사용하기 위해 코드를 작성하고 있습니다. Corey는 이전에 게시했습니다 좋은 샘플 코드 자르기 및 스케일링과 관련이 있습니다. 그러나 Cropimage의 구현이 없습니다 : to : andscaleto : nor straightenandscaleimage ().

그들이 사용하는 방법은 다음과 같습니다.

newImage =  [self cropImage:originalImage to:croppingRect andScaleTo:scaledImageSize];
...
UIImage *rotatedImage = straightenAndScaleImage([editInfo objectForKey:UIImagePickerControllerOriginalImage], scaleSize);

누군가 Corey의 샘플 코드와 매우 유사한 것을 사용해야한다고 확신하기 때문에이 두 기능의 기존 구현이있을 수 있습니다. 누군가 공유하고 싶습니까?

도움이 되었습니까?

해결책

링크 된 게시물을 확인하면이 코드 중 일부를 얻은 Apple Dev 포럼에 대한 링크가 표시됩니다. 다음은 요청하는 방법이 있습니다. 참고 : 데이터 유형과 관련하여 몇 가지 변경을했을 수도 있지만 기억할 수는 없습니다. 필요한 경우 조정하는 것은 사소한 일입니다.

- (UIImage *)cropImage:(UIImage *)image to:(CGRect)cropRect andScaleTo:(CGSize)size {
UIGraphicsBeginImageContext(size);
CGContextRef context = UIGraphicsGetCurrentContext();
CGImageRef subImage = CGImageCreateWithImageInRect([image CGImage], cropRect);
CGRect myRect = CGRectMake(0.0f, 0.0f, size.width, size.height);
CGContextScaleCTM(context, 1.0f, -1.0f);
CGContextTranslateCTM(context, 0.0f, -size.height);
CGContextDrawImage(context, myRect, subImage);
UIImage* croppedImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
CGImageRelease(subImage);
return croppedImage;

}

UIImage *straightenAndScaleImage(UIImage *image, int maxDimension) {

CGImageRef img = [image CGImage];
CGFloat width = CGImageGetWidth(img);
CGFloat height = CGImageGetHeight(img);
CGRect bounds = CGRectMake(0, 0, width, height);
CGSize size = bounds.size;
if (width > maxDimension || height > maxDimension) {
    CGFloat ratio = width/height;
    if (ratio > 1.0f) {
        size.width = maxDimension;
        size.height = size.width / ratio;
    }
    else {
        size.height = maxDimension;
        size.width = size.height * ratio;
    }
} 

CGFloat scale = size.width/width;
CGAffineTransform transform = orientationTransformForImage(image, &size);
UIGraphicsBeginImageContext(size);
CGContextRef context = UIGraphicsGetCurrentContext();
// Flip 
UIImageOrientation orientation = [image imageOrientation];
if (orientation == UIImageOrientationRight || orientation == UIImageOrientationLeft) {

    CGContextScaleCTM(context, -scale, scale);
    CGContextTranslateCTM(context, -height, 0);
}else {
    CGContextScaleCTM(context, scale, -scale);
    CGContextTranslateCTM(context, 0, -height);
}
CGContextConcatCTM(context, transform);
CGContextDrawImage(context, bounds, img);
UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return newImage;

}

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top