如何在iOS Swift应用程序中实现PIP效果。我尝试了但是在放置和移动内部图片的情况下结果不准确。 这是我的尝试:
@IBOutlet var galleryImageView: UIImageView!
@IBOutlet var background: UIImageView!
@IBOutlet var behindView: UIView!
override func viewDidLoad() {
super.viewDidLoad()
let image = UIImage(named: "ph3")
let maskingImage = UIImage(named: "mask")
galleryImageView.image = maskImage(image: image!, mask: maskingImage!)
galleryImageView.contentMode = .scaleAspectFill
background.image = UIImage(named: "ph3")?.stackBlur(10)
let maskingLayer = CALayer()
maskingLayer.frame = behindView.bounds
maskingLayer.contents = (galleryImageView.image?.cgImage! as Any)
behindView.layer.mask = maskingLayer
let panGestureRecognizer = UIPanGestureRecognizer(target: self, action: #selector(self.panGestureDetected))
panGestureRecognizer.delegate = self
self.galleryImageView.addGestureRecognizer(panGestureRecognizer)
}
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
}
galleryImageView是内部imageView
结果应该是: