1
我正在製作包含數碼變焦的相機應用程序。我有一個滑塊(zoomSlider),其最小值爲1,最大值爲4.當用戶點擊相機按鈕時,它會拍攝一張照片,然後裁剪出來進行縮放。我有兩個問題:從中心向外裁剪UIImage?
如何剪切圖像的中間部分? (例如2倍變焦,Rect將以尺寸爲600x800(適用於iPhone 2G/3G)爲中心)
當我這樣做時,它會旋轉圖像。我通過旋轉UIImageView來彌補它的不足,但這會導致人像照片變成風景,反之亦然。
這裏是我的代碼:
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {
if ([mediaType isEqualToString:@"public.image"]){
UIImage *image = [info objectForKey:@"UIImagePickerControllerOriginalImage"];
CGRect clippedRect = CGRectMake(600, 450, image.size.width/zoomSlider.value, image.size.height/zoomSlider.value);
UIImage *cropped = [self imageByCropping:image toRect:clippedRect];
CGRect croppedImageSize = CGRectMake(0, 0, image.size.width/zoomSlider.value, image.size.height/zoomSlider.value);
[cropped drawInRect:croppedImageSize];
zoomPhoto.frame = croppedImageSize;
zoomPhoto.image = cropped;
CGAffineTransform rotateTransform = CGAffineTransformRotate(CGAffineTransformIdentity,
RADIANS(90.0));
zoomPhoto.transform = rotateTransform;
}
- (UIImage *)imageByCropping:(UIImage *)imageToCrop toRect:(CGRect)rect
{
CGImageRef imageRef = CGImageCreateWithImageInRect([imageToCrop CGImage], rect);
UIImage *cropped = [UIImage imageWithCGImage:imageRef];
CGImageRelease(imageRef);
return cropped;
}