can i crop image by coordinates like (10,10,320,312)?

浪子不回头ぞ 提交于 2019-12-12 06:28:56

问题


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

after zoom and crop

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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!