如何将2张图像合并为1张

时间:2018-12-03 15:40:16

标签: swift cocoa

我正在尝试将裁剪后的照片粘贴到咖啡杯的纹理贴图上,上面写着“在此处放置徽标”,然后将该贴图应用于3d dae模型。我最初的尝试没有做任何事情。我不确定要在我的mapImage上绘制croppedImage

let croppedImage = NSImage.init(cgImage: croppedCGImage, size:NSSize.init(width: cropWidth, height: cropHeight))

//texture map
let mapImage = NSImage.init(named: "coffee_c.jpg")
let mapImageData = mapImage?.tiffRepresentation
let bMap:NSBitmapImageRep = NSBitmapImageRep(data: mapImageData!)!

NSGraphicsContext.init(bitmapImageRep: bMap)
mapImage?.draw(in: CGRect(x: cropX, y: cropY, width: cropWidth, height: cropHeight))
let newImage = NSGraphicsContext.current

enter image description here

1 个答案:

答案 0 :(得分:0)

如果您尝试通过绘图创建NSImage,则可以使用lockFocus / unlockFocus(在Safari中键入):

let newImage = NSImage(size: .../* probably the size of your original/background */)
newImage.lockFocus()
// draw the first image, draw something on top of that, etc.
newImage.unlockFocus()
// ... you can now draw newImage wherever you want

Cocoa Drawing Guide

中对此进行了介绍

如果您需要图像对不同的屏幕分辨率敏感,请考虑使用NSImage(size: NSSize, flipped: Bool, drawingHandler: @escaping (NSRect) -> Bool),它使用代码块在实际的渲染上下文中延迟创建图像。