如何将蒙版应用于保存图像(swift3)

时间:2017-04-04 14:20:36

标签: ios swift3 save photo mask

我的代码执行以下操作:

  1. 拍摄照片并在图像视图中显示。 enter image description here
  2. 使用照片库中的cgrect将图片视图保存为自定义尺寸。 enter image description here
  3. 但是我的问题是掩码没有转移到l?.draw(in: CGRect(x: 10, y: 240.5, width: 435 , height: 435), blendMode: .normal, alpha: 1.0)。我想做的就是用面具保存照片。

    import UIKit
    
    class ViewController: UIViewController, UIImagePickerControllerDelegate, UINavigationControllerDelegate {
    var screenView: UIImageView!
    
    var image1 = UIImage(named: "w")
    var image3 = UIImage(named: "w")
    var image2 = UIImage(named: "w")
    func cropImageIntoQuarterSquare(image: UIImage) -> UIImage? {
        let originalImageSize: CGSize = image.size
        let smallImageSize = CGSize(width: (originalImageSize.width + 40), height: (originalImageSize.height + 40))
        UIGraphicsBeginImageContext(smallImageSize)
        image.draw(at: CGPoint.zero)
        let imageResult = UIGraphicsGetImageFromCurrentImageContext()
        UIGraphicsEndImageContext()
        return imageResult
    }
    var currentImageView: UIImageView?
    @IBOutlet var photoDispaly: UIImageView!
    override func viewDidLoad() {
        super.viewDidLoad()
    
    }
    func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) {
        let image = info[UIImagePickerControllerOriginalImage] as! UIImage
        self.currentImageView?.image = image
        self.dismiss(animated: true)
    
    }
    func saveImage(image1: UIImage, image2: UIImage, image3: UIImage) -> UIImage? {
    
        screenView = UIImageView()
    
        let newSize = CGSize(width: 900, height: 1200)
        UIGraphicsBeginImageContextWithOptions(newSize, false, 0)
        defer { UIGraphicsEndImageContext() }
        UIColor.black.setFill()
        UIBezierPath(rect: CGRect(origin: .zero, size: newSize)).fill()
        UIGraphicsGetImageFromCurrentImageContext()
    
        let l = photoDispaly.image
    
        let image = photoDispaly.image
    
        let maskingImage = UIImage(named: "mask5")
    
        photoDispaly.image = maskImage(image: image!, mask: maskingImage!)
        photoDispaly.autoresizingMask = [.flexibleWidth, .flexibleHeight, .flexibleBottomMargin, .flexibleRightMargin, .flexibleLeftMargin, .flexibleTopMargin]
        photoDispaly.contentMode = .scaleAspectFit // OR .scaleAspectFill
        photoDispaly.clipsToBounds = true
    
        l?.draw(in: CGRect(x: 10, y: 240.5, width: 435 , height: 435), blendMode: .normal, alpha: 1.0)
    
        let newImage: UIImage = UIGraphicsGetImageFromCurrentImageContext()!
        UIGraphicsEndImageContext()
    
        screenView.frame = CGRect(x:0, y: 0, width: self.view.frame.width, height: self.view.frame.height)
    
        self.view.addSubview(screenView)
        return newImage
    }
    @IBAction func takePhot(_ sender: Any) {
        self.currentImageView = self.photoDispaly
        if UIImagePickerController.isSourceTypeAvailable(UIImagePickerControllerSourceType.camera){
            let imagePicker = UIImagePickerController()
            imagePicker.delegate = self
    
            imagePicker.sourceType = UIImagePickerControllerSourceType.camera;
            imagePicker.allowsEditing = false
            self.present(imagePicker, animated: true, completion: nil)
        }
    
    }
    
    func maskImage(image:UIImage, mask:(UIImage))->UIImage{
        let imageReference = image.cgImage
        let maskReference = mask.cgImage
        let imageMask = CGImage(maskWidth: maskReference!.width,
                                height: maskReference!.height,
                                bitsPerComponent: maskReference!.bitsPerComponent,
                                bitsPerPixel: maskReference!.bitsPerPixel,
                                bytesPerRow: maskReference!.bytesPerRow,
                                provider: maskReference!.dataProvider!, decode: nil, shouldInterpolate: true)
        let maskedReference = imageReference!.masking(imageMask!)
        let maskedImage = UIImage(cgImage:maskedReference!)
        return maskedImage
    }
    
    
    func image(_ image: UIImage, didFinishSavingWithError error: Error?, contextInfo: UnsafeRawPointer) {
        if let error = error {
              return
        } else {
            return
        }
    }
    
    
    @IBAction func savePhoto(_ sender: Any) {
    
        guard let croppedImage = cropImageIntoQuarterSquare(image: image2!),
            let combinedImage = saveImage(image1: image1!, image2: croppedImage, image3: croppedImage) else {
    
                return
        }
    
        UIImageWriteToSavedPhotosAlbum(combinedImage, self, #selector(image(_:didFinishSavingWithError:contextInfo:)), nil)
    
    }}
    

1 个答案:

答案 0 :(得分:0)

import UIKit

class ViewController: UIViewController, UIImagePickerControllerDelegate, UINavigationControllerDelegate {
var screenView: UIImageView!

var image1 = UIImage(named: "w")
var image3 = UIImage(named: "w")
var image2 = UIImage(named: "w")
func cropImageIntoQuarterSquare(image: UIImage) -> UIImage? {

    let image = photoDispaly.image

    let maskingImage = UIImage(named: "mask5")



    photoDispaly.image = maskImage(image: image!, mask: maskingImage!)
    photoDispaly.autoresizingMask = [.flexibleWidth, .flexibleHeight, .flexibleBottomMargin, .flexibleRightMargin, .flexibleLeftMargin, .flexibleTopMargin]
    photoDispaly.contentMode = .scaleAspectFit // OR .scaleAspectFill
    photoDispaly.clipsToBounds = true


    let originalImageSize: CGSize = image!.size
    let smallImageSize = CGSize(width: (originalImageSize.width + 40), height: (originalImageSize.height + 40))
    UIGraphicsBeginImageContext(smallImageSize)
    image?.draw(at: CGPoint.zero)
    let imageResult = UIGraphicsGetImageFromCurrentImageContext()
    UIGraphicsEndImageContext()
    return imageResult
}
var currentImageView: UIImageView?
@IBOutlet var photoDispaly: UIImageView!
override func viewDidLoad() {
    super.viewDidLoad()



}
 func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) {
    let image = info[UIImagePickerControllerOriginalImage] as! UIImage
    self.currentImageView?.image = image
    self.dismiss(animated: true)

}


func saveImage(image1: UIImage, image2: UIImage, image3: UIImage) -> UIImage? {



    screenView = UIImageView()

    let newSize = CGSize(width: 900, height: 1200)
    UIGraphicsBeginImageContextWithOptions(newSize, false, 0)
    defer { UIGraphicsEndImageContext() }
    UIColor.black.setFill()
    UIBezierPath(rect: CGRect(origin: .zero, size: newSize)).fill()
    UIGraphicsGetImageFromCurrentImageContext()



    let l = photoDispaly.image






      l?.draw(in: CGRect(x: 10, y: 240.5, width: 435 , height: 435), blendMode: .normal, alpha: 1.0)




    let newImage: UIImage = UIGraphicsGetImageFromCurrentImageContext()!
    UIGraphicsEndImageContext()

    screenView.frame = CGRect(x:0, y: 0, width: self.view.frame.width, height: self.view.frame.height)

    self.view.addSubview(screenView)
    return newImage
}
@IBAction func takePhot(_ sender: Any) {
    self.currentImageView = self.photoDispaly
    if UIImagePickerController.isSourceTypeAvailable(UIImagePickerControllerSourceType.camera){
        let imagePicker = UIImagePickerController()
        imagePicker.delegate = self




        imagePicker.sourceType = UIImagePickerControllerSourceType.camera;
        imagePicker.allowsEditing = false
        self.present(imagePicker, animated: true, completion: nil)


    }

}

func maskImage(image:UIImage, mask:(UIImage))->UIImage{
    let imageReference = image.cgImage
    let maskReference = mask.cgImage
    let imageMask = CGImage(maskWidth: maskReference!.width,
                            height: maskReference!.height,
                            bitsPerComponent: maskReference!.bitsPerComponent,
                            bitsPerPixel: maskReference!.bitsPerPixel,
                            bytesPerRow: maskReference!.bytesPerRow,
                            provider: maskReference!.dataProvider!, decode: nil, shouldInterpolate: true)
    let maskedReference = imageReference!.masking(imageMask!)
    let maskedImage = UIImage(cgImage:maskedReference!)
    return maskedImage
}



func image(_ image: UIImage, didFinishSavingWithError error: Error?, contextInfo: UnsafeRawPointer) {
    if let error = error {
            return
    } else {
        return

    }}


@IBAction func savePhoto(_ sender: Any) {

    guard let croppedImage = cropImageIntoQuarterSquare(image: image1!),
        let combinedImage = saveImage(image1: image1!, image2: croppedImage, image3: croppedImage)




        else {

            return
    }

    UIImageWriteToSavedPhotosAlbum(combinedImage, self, #selector(image(_:didFinishSavingWithError:contextInfo:)), nil)
    }}