作者:手浪用户2602897055 | 来源:互联网 | 2023-05-18 23:06
IvecreatedSwiftversionofthisclass:https:github.combennytheminkZoomRotatePanImageViewblo
I've created Swift version of this class: https://github.com/bennythemink/ZoomRotatePanImageView/blob/master/ZoomRotatePanImageView.m Works nice. Now I want to save modified image to file. The thing is I want to save it in full resolution and also I want to save area which is only visible to user.
我已经创建了这个类的Swift版本:https://github.com/bennythemink/ZoomRotatePanImageView/blob/master/ZoomRotatePanImageView.m效果很好。现在我想将修改后的图像保存到文件中。问题是我想以全分辨率保存它,而且我想保存只对用户可见的区域。
Let me show you simple example:
让我举个简单的例子:
This is how it looks in my app. Image is one a few samples in iOS simulator. Most of it is out of screen. I want only visible part.
这就是它在我的应用程序中的外观。图像是iOS模拟器中的一些示例。大部分是在屏幕外。我只想看到可见的部分。
After saving without cropping it looks like this:
保存后没有裁剪它看起来像这样:
So far so good after clipping it'd be nice.
到目前为止,剪辑后它很好,这很好。
But now let's make some changes:
但现在让我们做一些改变:
After saving:
保存后:
Looks like it's transformed by wrong pivot. How can I fix it?
看起来它被错误的支点所改变。我该如何解决?
Here's my code for saving:
这是我保存的代码:
UIGraphicsBeginImageContextWithOptions(image.size, false, 0)
let cOntext= UIGraphicsGetCurrentContext()
let transform = imageView.transform
let imageRect = CGRectMake(0, 0, image.size.width, image.size.height)
CGContextSetFillColorWithColor(context, UIColor.blueColor().CGColor) //for debugging
CGContextFillRect(context, imageRect)
CGContextConcatCTM(context, transform)
image.drawInRect(imageRect)
let newImage = UIGraphicsGetImageFromCurrentImageContext()!
UIGraphicsEndImageContext()
There's a simpler method to achieve it:
有一种更简单的方法来实现它:
UIGraphicsBeginImageContextWithOptions(imageContainer.bounds.size, false, 0)
self.imageContainer.drawViewHierarchyInRect(imageContainer.bounds, afterScreenUpdates: true)
let screenshot = UIGraphicsGetImageFromCurrentImageContext()!
UIGraphicsEndImageContext()
However output image has size of the view not an actual image and I want it in full resolution.
但是输出图像的视图大小不是实际图像,我希望它以全分辨率。
1 个解决方案