旋转和收缩手势进行编辑后如何保存UIImage

旋转和收缩手势进行编辑后如何保存UIImage

本文介绍了使用平移,旋转和收缩手势进行编辑后如何保存UIImage的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用UIPanGesture,UIRotateGesture和UIPinchGesture来实现编辑图像.编辑后,我希望保存编辑后的UIImage.

I'm trying to implement edit image using UIPanGesture, UIRotateGesture, and UIPinchGesture. After editing, I wish to save edited UIImage.

我已经完成了图片编辑功能.

I'm done edit image functionality.

但是我不知道如何像原生iOS Camera App一样保存完美.

But I don't know How to save exactly perfect like native iOS Camera App.

Pan Gesture工作正常.

Pan Gesture is working fine.

我的问题是旋转和缩放.

My problem is Rotate and Scale.

如何设置UIImage的中心锚点?

How to set center anchor point of UIImage?

当我从转换信息中保存UIImage时,由于旋转和缩放,UIImage的结果看起来有所不同,因此它始终基于(0,0)应用

When I saved UIImage from transform information then the result of UIImage is looks different because Rotate and Scale, then It always applied based on (0,0)

 @IBAction func showEditedImage(_ sender: Any) {


        let image = preview.image!
        var drawingRect : CGRect = CGRect.zero
        drawingRect.size = preview.frame.size

        let scale = transformedImg.frame.width / (preview.image?.size.width)!
        let height = (preview.image?.size.height)! * scale

        UIGraphicsBeginImageContextWithOptions(CGSize(width: transformedImg.frame.width, height: height), false, 0)

        let context = UIGraphicsGetCurrentContext()
        context!.concatenate(preview.transform)

        image.draw(in: CGRect(x: 0, y: 0, width: transformedImg.frame.width, height: height))
        resultsImg = UIGraphicsGetImageFromCurrentImageContext()
        UIGraphicsEndImageContext();

        transformedImg.image = resultsImg
    }

这是我所有的代码.

import UIKit

class ViewController: UIViewController {


    @IBOutlet weak var preview: UIImageView!
    @IBOutlet weak var frame: UIView!
    @IBOutlet weak var transformedImg: UIImageView!

    //save edited image
    var resultsImg : UIImage!

    override func viewDidLoad() {

        resultsImg = UIImage()
        super.viewDidLoad()
    }


       @IBAction func showEditedImage(_ sender: Any) {


    let image = preview.image!
    var drawingRect : CGRect = CGRect.zero
    drawingRect.size = preview.frame.size

    let scale = transformedImg.frame.width / (preview.image?.size.width)!
    let height = (preview.image?.size.height)! * scale

    UIGraphicsBeginImageContextWithOptions(CGSize(width: transformedImg.frame.width, height: height), false, 0)

    let context = UIGraphicsGetCurrentContext()
    context!.concatenate(preview.transform)

    image.draw(in: CGRect(x: 0, y: 0, width: transformedImg.frame.width, height: height))
    resultsImg = UIGraphicsGetImageFromCurrentImageContext()
    UIGraphicsEndImageContext();

    transformedImg.image = resultsImg
}

    @IBAction func pinchaction(_ sender: UIPinchGestureRecognizer) {

        preview.transform = preview.transform.scaledBy(x: sender.scale, y: sender.scale)

        sender.scale = 1
    }

    @IBAction func rotateaction(_ sender: UIRotationGestureRecognizer) {

        let rotate = sender.rotation
        preview.transform = preview.transform.rotated(by: rotate)


        sender.rotation = 0

    }

    @IBAction func panaction(_ sender: UIPanGestureRecognizer) {

        let points = sender.translation(in: self.frame)
        preview.transform = preview.transform.translatedBy(x: points.x, y: points.y)

        let translatedCenter = CGPoint(x:self.frame.center.x + points.x, y:self.frame.center.y + points.y)

        sender.setTranslation(CGPoint.zero, in: self.frame)

    }



}

环境:XCode9 + Swift 4

Environment : XCode9 + Swift 4

2017年10月14日更新

Updated 14 October 2017

我解决了我的问题

 @IBAction func showEditedImage(_ sender: Any) {

        let image = preview.image!
        var drawingRect : CGRect = CGRect.zero
        drawingRect.size = preview.frame.size

        let scale = transformedImg.frame.width / (preview.image?.size.width)!
        let height = (preview.image?.size.height)! * scale


        UIGraphicsBeginImageContextWithOptions(CGSize(width: transformedImg.frame.width, height: height), false, 0)

        let context = UIGraphicsGetCurrentContext()

        //Set Center Position before Scale, Rotate Image
        context?.translateBy(x: transformedImg.frame.width / 2, y: height / 2)

        //Applied Translate, Scale, Rotate
        context!.concatenate(preview.transform)

        context?.translateBy(x: -(transformedImg.frame.width / 2), y: -(height / 2))

        image.draw(in: CGRect(x: 0, y: 0, width: transformedImg.frame.width, height: height))
        resultsImg = UIGraphicsGetImageFromCurrentImageContext()
        UIGraphicsEndImageContext();

        transformedImg.image = resultsImg
    }

推荐答案

我有一个名为myView且具有透明背景的UIIView,我将UIImageView添加到myView中作为子视图,然后在myView上应用UIRotationGestureRecognizer,以便在此处旋转我的图像是我的@IBActionUIRotationGestureRecognizer的功能....

I have a UIIView named myView with Transparent background and i add UIImageView into myView as subview then i apply UIRotationGestureRecognizer on myView so that rotate my Image here is my @IBAction func of UIRotationGestureRecognizer....

@IBAction func rotateGesture(sender: UIRotationGestureRecognizer) {
    if let view = sender.view {
        view.transform = CGAffineTransformRotate(view.transform, sender.rotation)
        sender.rotation = 0
    }
}

这篇关于使用平移,旋转和收缩手势进行编辑后如何保存UIImage的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-03 14:37