热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

在缩放,旋转和平移后保存已编辑的图像-Savingeditedimageafterzooming,rotatingandpanning

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:

让我举个简单的例子:

enter image description here

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:

保存后没有裁剪它看起来像这样:

enter image description here

So far so good after clipping it'd be nice.

到目前为止,剪辑后它很好,这很好。

But now let's make some changes:

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

enter image description here

After saving:

保存后:

enter image description here

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 个解决方案

#1


4  

Updated code to work with image of any size :

更新代码以处理任何大小的图像:

     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 screen shot Simulator

模拟器屏幕截图

Saved image Saved

保存的图像


推荐阅读
author-avatar
手浪用户2602897055
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有