我可以通过像(10,10,320,312)这样的坐标裁剪图像吗?

时间:2012-07-03 06:32:52

标签: iphone ios crop

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

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

谢谢!!

缩放前

缩放并裁剪before zooming

after zoom and crop

我的代码在这里。

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

1 个答案:

答案 0 :(得分:1)

愿这段代码全部为您使用.. !!

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