我创建了一个应用程序,在其中我可以使用手势旋转、调整大小、平移图像。然后我需要从 UIImageView
获取图像。我在Stack-overflow的某个地方找到了这部分代码。虽然微笑者的问题得到了解答here ,但需要输入角度。同一个人在其他地方写了更好的解决方案,我正在使用它。但它有一个问题。通常它会返回空白图像。或截断的图像(通常从顶部)。所以代码有问题,需要进行一些更改。我的问题是,我是核心显卡的新手,并且严重陷入这个问题。
UIGraphicsBeginImageContext(imgView.image.size);
CGContextRef context = UIGraphicsGetCurrentContext();
CGAffineTransform transform = imgView.transform;
transform = CGAffineTransformScale(transform, 1.0, -1.0);
CGContextConcatCTM(context, transform);
CGContextDrawImage(context, CGRectMake(0, 0, imgView.image.size.width, imgView.image.size.height), imgView.image.CGImage);
UIImage *newRotatedImage = [UIImage imageWithCGImage: CGBitmapContextCreateImage(context)];
UIGraphicsEndImageContext();
编辑1.1 感谢您提供示例代码,但它再次出现问题。让我更详细地解释一下,我使用 imageview 使用手势来缩放、翻译和调整图像大小。所以所有这些数据都保存在imageview的transform属性中。我喜欢 core-image 中的另一种方法。所以我将代码更改为:
CGRect bounds = CGRectMake(0, 0, imgTop.size.width, imgTop.size.height);
CIImage *ciImage = [[CIImage alloc] initWithCGImage:imageView.image.CGImage options:nil];
CGAffineTransform transform = imgView.transform;
ciImage = [ciImage imageByApplyingTransform:transform];
return [UIImage imageWithCIImage:ciImage] ;
现在我得到了被挤压且尺寸错误的镜像。抱歉再次打扰您。你能指导我如何使用 coreimage 中的 imageview 转换来获取正确的图像吗?
最佳答案
CIImage *ciImage = [[CIImage alloc] initWithCGImage:fximage.CGImage options:nil];
CGAffineTransform transform = fxobj.transform;
float angle = atan2(transform.b, transform.a);
transform = CGAffineTransformRotate(transform, - 2 * angle);
ciImage = [ciImage imageByApplyingTransform:transform];
UIImage *screenfxImage = [UIImage imageWithCIImage:ciImage];
记得添加代码:transform = CGAffineTransformRotate(transform, - 2 * angle);
因为旋转方向是相反的
关于objective-c - 从 uiimageview 获取旋转后的图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10033303/