gpt4 book ai didi

ios - UIScrollView 中图像上的 cropImage 方法问题产生错误的图像

转载 作者:行者123 更新时间:2023-11-29 00:57:06 25 4
gpt4 key购买 nike

我正在使用以下裁剪方法裁剪位于 UIImageView 中的 uiimage,然后该 uiimage 位于 UIScrollView 中。

-(UIImage *)cropImage:(UIImage *)image
{
float scale = 1.0f/_scrollView.zoomScale;

NSLog(@"Oh and heres that zoomScale: %f", _scrollView.zoomScale);

CGRect visibleRect;
visibleRect.origin.x = _scrollView.contentOffset.x * scale;
visibleRect.origin.y = _scrollView.contentOffset.y * scale;
visibleRect.size.width = _scrollView.bounds.size.width * scale;
visibleRect.size.height = _scrollView.bounds.size.height * scale;

NSLog(@"Oh and here's that CGRect: %f", visibleRect.origin.x);
NSLog(@"Oh and here's that CGRect: %f", visibleRect.origin.y);
NSLog(@"Oh and here's that CGRect: %f", visibleRect.size.width);
NSLog(@"Oh and here's that CGRect: %f", visibleRect.size.height);

CGImageRef imageRef = CGImageCreateWithImageInRect([image CGImage], visibleRect);
UIImage *croppedImage = [[UIImage alloc] initWithCGImage:imageRef];
CGImageRelease(imageRef);

return croppedImage;
}

我需要将图像裁剪为 (321,115) 的 CGSize。裁剪图像并查看打印结果后,我可以看到visibleRect是(0,0,321,115)——它应该是什么,croppedImage UIImage的宽度为:321,高度为:115。然而,由于某种原因,图像似乎被完全放大得太远(该方法将原始图像的一小部分裁剪为 321x115 的大小)。

为什么这种方法不能正确裁剪我的图像?

-作为旁注:当我调用此方法时,我会像这样调用 _croppedImage = [self CroppedImage:_imageView.image]; ,它将自定义 UIView 类的 UIImage 属性设置为裁剪后的图像。

最佳答案

请尝试此功能。它可能对您有所帮助。

参数:

  1. UIImage
  2. CGSize (321,115) 或任意大小

//裁剪图像 - 图像将从完整图像裁剪

- (UIImage *)cropImageWithImage:(UIImage *)image scaledToSize:(CGSize)newSize {
double ratio;
double delta;
CGPoint offset;

//make a new square size, that is the resized imaged width
CGSize sz = CGSizeMake(newSize.width, newSize.width);

//figure out if the picture is landscape or portrait, then
//calculate scale factor and offset
if (image.size.width > image.size.height) {
ratio = newSize.width / image.size.width;
delta = (ratio*image.size.width - ratio*image.size.height);
offset = CGPointMake(delta/2, 0);
}
else {
ratio = newSize.width / image.size.height;
delta = (ratio*image.size.height - ratio*image.size.width);
offset = CGPointMake(0, delta/2);
}

//make the final clipping rect based on the calculated values
CGRect clipRect = CGRectMake(-offset.x,
-offset.y,
(ratio * image.size.width) + delta,
(ratio * image.size.height) + delta);


//start a new context, with scale factor 0.0 so retina displays get
//high quality image
if ([[UIScreen mainScreen] respondsToSelector:@selector(scale)]) {
UIGraphicsBeginImageContextWithOptions(sz, YES, 0.0);
} else {
UIGraphicsBeginImageContext(sz);
}
UIRectClip(clipRect);
[image drawInRect:clipRect];
UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();

return newImage;
}

仅裁剪图像的选定部分

请查看this link

关于ios - UIScrollView 中图像上的 cropImage 方法问题产生错误的图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37517521/

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