Cropping CIImage

后端 未结 2 1145
天命终不由人
天命终不由人 2020-12-18 06:22

I have a class that takes an UIImage, initializes a CIImage with it like so:

workingImage = CIImage.init(image: baseImage!)
         


        
2条回答
  •  春和景丽
    2020-12-18 06:33

    The heart of the matter is that passing through CIImage is not the way to crop a UIImage. For one thing, coming back from CIImage to UIImage is a complicated business. For another, the whole round-trip is unnecessary.

    How To Crop

    To crop an image, make an image graphics context of the desired cropped size and call draw(at:) on the UIImage to draw it at the desired point relative to the graphics context, so that the desired portion of the image falls into the context. Now extract the resulting new image and close the context.

    To demonstrate, I'll crop to one of the thirds you are trying to crop to, namely the lower right third:

    let sz = baseImage.size
    UIGraphicsBeginImageContextWithOptions(
        CGSize(width:sz.width/3.0, height:sz.height/3.0), 
        false, 0)
    baseImage.draw(at:CGPoint(x: -sz.width/3.0*2.0, y: -sz.height/3.0*2.0))
    let tmpImg = UIGraphicsGetImageFromCurrentImageContext()
    UIGraphicsEndImageContext()
    

    Original image (baseImage):

    Cropped image (tmpImg):

    The other sections are completely parallel.

提交回复
热议问题