How to stretch image into custom shape (swift3)

拈花ヽ惹草 提交于 2019-12-24 04:36:06

问题


The code list below crops the image. I would like the image to be transformed/ stretched and not cropped in the shape. All of the image contents are still in the transformed picture. It will just look different.

extension UIImageView {
func addMask(_ bezierPath: UIBezierPath) {
    let pathMask = CAShapeLayer()
    pathMask.path = bezierPath.cgPath
    layer.mask = pathMask
}
  }


     let myPicture = UIImage(data: try! Data(contentsOf: URL(string:"https://scontent-iad3-1.xx.fbcdn.net/v/t31.0-8/14196150_10207770295835716_3510332720585393213_o.jpg?oh=fdb525410602f40f4735991b713e9c50&oe=596688E5")!))!
    let iv = UIImageView(image: myPicture)

  let bezierPath = UIBezierPath()
   bezierPath.move(to: iv.center)
  bezierPath.addLine(to: CGPoint(x: iv.frame.maxX, y: 0))
  bezierPath.addLine(to: CGPoint(x: iv.frame.maxX, y: iv.frame.maxY))
  bezierPath.addLine(to: CGPoint(x: 0, y: iv.frame.maxY))
  bezierPath.addLine(to: .zero)
  bezierPath.close()

 iv.addMask(bezierPath)

回答1:


Take a look at this thread:

Warp \ bend effect on a UIView?

There are several solutions discussed, including using a Core Image filter, or building your own "mesh warp" using OpenGL.

There's also the Brad Larson open source framework GPUImage on Github. You might be able to build a filter with that framework to do what you want.




回答2:


when you have to display that type image you can use image filter core or you can try image masking..

Image core Filter: show in:

https://www.appcoda.com/core-image-introduction

https://code.tutsplus.com/tutorials/ios-sdk-apply-photo-filters-with-core-image-in-swift--cms-27142

Image Masking:

https://www.innofied.com/implementing-image-masking-in-ios/

i am try my best..to provide solution..that is simple image

that is mask image that is gif type image

code that write

override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.
        let image = UIImage(named: "image.gif")
        let maskingImage = UIImage(named: "mask-image.gif")
        imageView.image = maskImage(image: image!, mask: maskingImage!)
    }

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
}

Final Result



来源:https://stackoverflow.com/questions/42861078/how-to-stretch-image-into-custom-shape-swift3

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!