gpt4 book ai didi

iphone - 我可以按 (10,10,320,312) 之类的坐标裁剪图像吗?

转载 作者:行者123 更新时间:2023-11-28 22:53:32 25 4
gpt4 key购买 nike

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

我在 UIScrollView 中有一个 UIImageView,我可以缩放图像。当我按下 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)

最佳答案

希望这段代码能为您充分利用..!!

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

关于iphone - 我可以按 (10,10,320,312) 之类的坐标裁剪图像吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11305606/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com