简体   繁体   中英

How to stretch image into custom shape (swift3)

在此处输入图片说明

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)

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.

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 在此处输入图片说明

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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