2009-05-15 4 views
0

私はUIImagePickerControllerを使用するコードを書いています。 Coreyは以前にクロッピングとスケーリングに関するsome nice sample code on SOを投稿しました。ただし、cropImage:to:andScaleTo:やstraightenAndScaleImage()の実装はありません。ここでcropImageの既存の実装:to:andScaleTo:およびstraightenAndScaleImage()?

は、それらが使用されている方法は次のとおりです。

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

私は誰かがコーリーのサンプルコードと非常によく似たものを使用しなければならないと確信しているので、これら2つの関数の既存の実装は、おそらくあります。誰かが共有したいですか?

+0

幸いにも私はあなたが前の回答/質問にコメントすることができ、私は –

+0

感謝それを見ることができます将来的に....新しい質問を通じて今夜breezingました君は!当時、私はコメントするには十分な評判がなかった。また、それはやや異なる質問だったので、別々に提示することができました。 – Elliot

答えて

1

あなたがにリンクされているポストを確認した場合、あなたは私はここに、このコードの一部を持って、あなたがについて尋ねている方法であるリンゴの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; 

}

関連する問題