0

目标:缩放后如何裁剪图像。我可以在(10,10,320,312)等预定义坐标处缩放后裁剪图像吗?我的 UIImageView 有这个尺寸吗?

我有一个UIImageViewUIScrollView我可以放大我的图像。当我按下cropButton正在显示的图像时,我希望在缩放后被UIImage裁剪。我想要图像的大小等于UIImageView大小。

谢谢 !!

缩放前 缩放
缩放前 和裁剪后 缩放和裁剪后

我的代码在这里。

-(void)cropButtonClicked
{   
    //Calculate the required area from the scrollview
    CGRect visibleRect;
    float scale = 1.0f/scroll.zoomScale;
    visibleRect.origin.x = scroll.contentOffset.x * scale;
    visibleRect.origin.y = scroll.contentOffset.y * scale;
    visibleRect.size.width = scroll.bounds.size.width * scale;
    visibleRect.size.height = scroll.bounds.size.height * scale;

    UIImage *image = [self imageByCropping:imageView.image toRect:visibleRect];

    imageView.image=image;


}


- (UIImage*)imageByCropping:(UIImage *)myImage toRect:(CGRect)cropToArea{
CGImageRef cropImageRef = CGImageCreateWithImageInRect(myImage.CGImage, cropToArea);
UIImage* cropped = [UIImage imageWithCGImage:cropImageRef];

CGImageRelease(cropImageRef);
return cropped;
}
I use this logic.. Am I RIGHT? (May be i have to check my ScrollView programming)
4

1 回答 1

2

愿这段代码为你使用完整..!!

CGSize itemSize = CGSizeMake(newWidth, newHeight);<br>
UIGraphicsBeginImageContextWithOptions(itemSize, NO, 0);<br>
CGRect imageRect = CGRectMake(0, 0, newWidth, newHeight);<br>
[image drawInRect:imageRect];<br>
UIImage* newImage = UIGraphicsGetImageFromCurrentImageContext();<br>
UIGraphicsEndImageContext();
于 2012-07-03T07:10:32.040 回答