2016-02-08 107 views
9

我创建了这个类的Swift版本:https://github.com/bennythemink/ZoomRotatePanImageView/blob/master/ZoomRotatePanImageView.m工程很不错。现在我想将修改后的图像保存到文件中。事情是我想保存在全分辨率,我也想保存只有用户可见的区域。缩放,旋转和平移后保存编辑后的图像

让我告诉你简单的例子:

enter image description here

这是怎么看我的应用程序。图像是iOS模拟器中的一些示例。它大部分是在屏幕外。我只想要可见的部分。

节省不裁剪,它看起来像在此之后:

enter image description here

到目前为止裁剪它会是不错的经过这么好。

但是,现在让我们做一些改变:

enter image description here

保存后:

enter image description here

看起来它是由错误的旋转变换。我该如何解决它?

这是我为节省代码:

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

有一个简单的方法来实现它:

UIGraphicsBeginImageContextWithOptions(imageContainer.bounds.size, false, 0) 
self.imageContainer.drawViewHierarchyInRect(imageContainer.bounds, afterScreenUpdates: true) 
let screenshot = UIGraphicsGetImageFromCurrentImageContext()! 
UIGraphicsEndImageContext() 

但是输出图像视图的大小不是实际的图像,我希望它在全解析度。

回答

4

更新代码与任何尺寸的图像的工作:

 let boundsScale = imageView.bounds.size.width/imageView.bounds.size.height 
    let imageScale = image!.size.width/image!.size.height 

    let size = (image?.size)! 

    var canvasSize = size 

    if boundsScale > imageScale { 
     canvasSize.width = canvasSize.height * boundsScale 
    }else{ 
     canvasSize.height = canvasSize.width/boundsScale 
    } 

    let xScale = canvasSize.width/imageView.bounds.width 
    let yScale = canvasSize.height/imageView.bounds.height 

    let center = CGPointApplyAffineTransform(imageView.center, CGAffineTransformScale(CGAffineTransformIdentity, xScale, yScale)) 

    let xCenter = center.x 
    let yCenter = center.y 

    UIGraphicsBeginImageContextWithOptions(canvasSize, false, 0); 
    let context = UIGraphicsGetCurrentContext()! 

    //Apply transformation 
    CGContextTranslateCTM(context, xCenter, yCenter) 

    CGContextConcatCTM(context, imageView.transform) 

    CGContextTranslateCTM(context, -xCenter, -yCenter) 

    var drawingRect : CGRect = CGRectZero 
    drawingRect.size = canvasSize 

    //Transaltion 
    drawingRect.origin.x = (xCenter - size.width*0.5) 
    drawingRect.origin.y = (yCenter - size.height*0.5) 

    //Aspectfit calculation 
    if boundsScale > imageScale { 
     drawingRect.size.width = drawingRect.size.height * imageScale 
    }else{ 
     drawingRect.size.height = drawingRect.size.width/imageScale 
    } 

    image!.drawInRect(drawingRect) 

    let newImage = UIGraphicsGetImageFromCurrentImageContext() 
    UIGraphicsEndImageContext() 

模拟器屏幕截图Simulator

保存的图像Saved

+0

感谢您的回答,但就像我说我要在全分辨率图像。我已经在类似屏幕的资源中发布了一个解决方案,请参阅我的问题的底部。 image!.drawInRect(imageView!.bounds) - 这会导致4k图像成为imageView的大小,因此它取决于屏幕大小。 – Makalele

+0

谢谢,我想我们很接近。它看起来应该像它应该。但问题是它需要正确裁剪到屏幕上的可见部分(准确地说,少一点 - 以适应iPhone图像)。请看全班代码: http://pastebin.com/6UUj1B0w in prepareForSegue是你的代码。 topImage是您可以在截图中看到的iPhone。您可以在空白视图控制器中的新项目上轻松进行测试。底部是唯一的方法 - 它显示它应该是什么样子。 – Makalele

+0

ZoomRotatePanImageView始终保持宽高比。所以不需要为你的topImageView和imageView设置框架,保持全屏尺寸。将内容模式设置为适合您的topImageView。从我的样本中截取屏幕截图,按预期工作。 –