如何旋转蒙面图像?

时间:2017-04-04 21:11:20

标签: ios swift rotation imageview mask

现在我的代码显示了这样的图像

enter image description here 如果我可以将图像顺时针旋转90度,它会工作。我该怎么做代码如下。我拿出了一些不必要的代码行。

import UIKit

class ViewController: UIViewController, UIImagePickerControllerDelegate, UINavigationControllerDelegate {
    var screenView: UIImageView!
    var image1 = UIImage(named: "w")

    func cropImageIntoQuarterSquare(image: UIImage) -> UIImage? {
        //shape of mask
        let image = photoDispaly.image
        let maskingImage = UIImage(named: "mask5")

        //where the masking code starts
        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
    }

    @IBOutlet var photoDispaly: UIImageView!

    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
        }
    }

1 个答案:

答案 0 :(得分:0)

您可以在ViewDidLoad函数中对UIImageView运行转换

numpy

您也可以将Dispaly重命名为Display;)