问题
Goal : How Crop a image after zooming. can i crop image after zooming at predefined coordinates like (10,10,320,312). my UIImageView has this size?
I have a UIImageView
in a UIScrollView
and I can zoom my image. I wants after zooming when I press cropButton
the image which is displaying in UIImage
is cropped. I wants size of image equal to UIImageView
size.
thanks !!
before zooming


my code is here.
-(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:
May this code use full for you..!!
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();
来源:https://stackoverflow.com/questions/11305606/can-i-crop-image-by-coordinates-like-10-10-320-312