简体   繁体   English

结合使用UIImageView和UIView并将其保存为图像

[英]combining UIImageView with UIView and save it as a Image

I am writing a small app that takes a photo and do modifications to it. 我正在编写一个小型应用程序,可以拍照并对其进行修改。 There is a feature to put stickers(images) on top of the taken photo. 有一个功能可以将贴纸(图像)放在拍摄的照片上。 I want the user to be abel to pinch rotate and drag the stickers so I used a UIImageView to contain the image so that I could use gesture functions to modify it. 我希望用户能捏捏旋转并拖动贴纸,所以我使用UIImageView包含图像,以便可以使用手势功能对其进行修改。

But here is the problem, after the user finished modifying the stickers, how could I save the photo with the sticker? 但是,问题出在用户修改完贴纸后,如何保存带有贴纸的照片? They are in different views and the only thing I can think of is to keep track of the modifications to the stickers and draw them on the photo after the user finished modifying it. 他们有不同的看法,我唯一能想到的就是跟踪用户对贴纸的修改,并在用户完成修改后将其绘制在照片上。 Is there a easier way? 有没有更简单的方法? What should I do? 我该怎么办?

func addSticker(name: String)
{
    var stickerModView = UIImageView(frame: CGRect(blah blah))
    var sticker = UIImage(named:"blah blah.png")
    stickerModView.image = sticker
    self.view.addSubview(stickerMod)

    var tapRec = UITapGestureRecognizer()
    var pinchRec = UIPinchGestureRecognizer()
    var rotateRec = UIRotationGestureRecognizer()
    var panRec = UIPanGestureRecognizer()

    pinchRec.addTarget(self, action: Selector("pinchedView:"))
    rotateRec.addTarget(self, action: Selector("rotatedView:"))
    panRec.addTarget(self, action: Selector("draggedView:"))
    tapRec.addTarget(self, action: Selector("tappedView:"))

    stickerModView.addGestureRecognizer(pinchRec)
    stickerModView.addGestureRecognizer(rotateRec)
    stickerModView.addGestureRecognizer(panRec)
    stickerModView.userInteractionEnabled = true
    stickerModView.multipleTouchEnabled = true

}

You can take a screen shot of the view. 您可以对视图进行截屏。 There are many examples of how to do that. 有许多如何做到这一点的例子

After adding your complete UIImageView with editing, you can try this, 添加完完整的UIImageView并进行编辑后,您可以尝试执行此操作,

    let rect : CGRect = CGRect() //Your view size from where you want to make UIImage
    UIGraphicsBeginImageContext(rect.size);
    let context : CGContextRef = UIGraphicsGetCurrentContext()
    self.view.layer.renderInContext(context)
    let img : UIImage  = UIGraphicsGetImageFromCurrentImageContext()
    UIGraphicsEndImageContext();
    //your image ready to save in img

It may help you!! 它可以帮助您!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM