简体   繁体   中英

How do I use drawInRect in Swift?

I'm attempting to blend two images together for a photo app and it calls for using drawInRect. This seemed like a simple concept in Obj-C, but I'm having issues wrapping my head around it in Swift for some reason.

This is one of MANY attempts by me to get this working:

secondImage.drawInRect(((origin.x * xScale), (origin.y * yScale), size.width * xScale, size.height * yScale, blendMode: kCGBlendModeNormal, alpha: 1.0)

I've tried other ways, but I keep getting told that I can't use CGFloats or CGPoints (which seems odd to me) and I'm not sure how else to express this. The Apple Documentation seems unclear to me: https://developer.apple.com/library/ios/documentation/UIKit/Reference/UIImage_Class/#//apple_ref/occ/instm/UIImage/drawInRect :

But maybe it's just me. Can anyone tell me what parameters I need to give drawInRect?

drawInRect: takes in a CGRect as a parameter (as the name implies)

use

secondImage.drawInRect(CGRect(x: <#CGFloat#>, y: <#CGFloat#>, width: <#CGFloat#>, height: <#CGFloat#>), blendMode: kCGBlendModeNormal, alpha: 1.0)

in the parameter

在swift 4中, drawInRect被重命名为:

your_image.draw(in: CGRect, blendMode: CGBlendMode, alpha: CGFloat)

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