首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在OS上使用Swift混合两个NSImages

在OS上使用Swift混合两个NSImages
EN

Stack Overflow用户
提问于 2015-04-06 08:55:15
回答 2查看 1.2K关注 0票数 1

我有两个NSImage实例。我希望image2放在image1之上,具有一定程度的不透明度。它们的尺寸是匹配的。这两个图像都不需要在UI中可见。

我如何才能正确地设置一个图形上下文,并绘制图像到它,一个完全不透明,另一个半透明?

我在这里读过几个答案,但我发现它很复杂,尤其是因为大多数答案似乎都是针对目标C或只适用于iOS。任何指针都是见习的。如果这可以在不需要CGContext的情况下完成,那就更好了。

代码语言:javascript
复制
func blendImages(image1: NSImage, image2: NSImage, alpha: CGFloat) -> CGImage {

    // Create context
    var ctx: CGContextRef = CGBitmapContextCreate(0, inputImage.size.width, inputImage.size.height, 8, inputImage.size.width*4, NSColorSpace.genericRGBColorSpace(), PremultipliedLast)
    let area = CGRectMake(0, 0, inputImage.size.width, inputImage.size.height)
    CGContextScaleCTM(ctx, 1, -1)

    // Draw image1 in context

    // Draw image2 with alpha opacity
    CGContextSetAlpha(ctx, CGFloat(0.5))

    // Create CGImage from context
    let outputImage = CGBitmapContextCreateImage(ctx)

    return outputImage
}

在其他地方,我有这个扩展可以从我的CGImages中获得NSImages:

代码语言:javascript
复制
extension NSImage {
    var CGImage: CGImageRef {
        get {
            let imageData = self.TIFFRepresentation
            let source = CGImageSourceCreateWithData(imageData as! CFDataRef, nil)
            let maskRef = CGImageSourceCreateImageAtIndex(source, 0, nil)
            return maskRef
        }
    }
}
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2015-04-15 06:35:11

我设法用NSGraphicsContext解决了这个问题。

代码语言:javascript
复制
func mergeImagesAB(pathA: String, pathB: String, fraction: CGFloat) -> CGImage {

    guard let imgA = NSImage(byReferencingFile: pathA),
         let imgB = NSImage(byReferencingFile: pathB),
         let bitmap = imgA.representations[0] as? NSBitmapImageRep,
         let ctx = NSGraphicsContext(bitmapImageRep: bitmap)
         else {fatalError("Failed to load images.")}

    let rect = NSRect(origin: CGPoint(x: 0, y: 0), size: bitmap.size)

    NSGraphicsContext.saveGraphicsState()
    NSGraphicsContext.setCurrentContext(ctx)
    imgB.drawInRect(rect, fromRect: rect, operation: .CompositeSourceOver, fraction: fraction)
    NSGraphicsContext.restoreGraphicsState()

    guard let result = bitmap.CGImage
        else {fatalError("Failed to create image.")}

    return result;
}
票数 3
EN

Stack Overflow用户

发布于 2017-04-28 15:31:55

谢谢亨里克,我需要这个给奥比杰-C。这是一个版本,以防有人也需要:

代码语言:javascript
复制
-(CGImageRef) mergeImage:(NSImage*)a andB:(NSImage*)b fraction:(float)fraction{
    NSBitmapImageRep *bitmap = (NSBitmapImageRep*)[[a representations] objectAtIndex:0];
    NSGraphicsContext *ctx = [NSGraphicsContext graphicsContextWithBitmapImageRep:bitmap];
    [NSGraphicsContext saveGraphicsState];
    [NSGraphicsContext setCurrentContext:ctx];

    CGRect rect = CGRectMake(0, 0, bitmap.size.width, bitmap.size.height);
    [NSGraphicsContext saveGraphicsState];
    [NSGraphicsContext setCurrentContext:ctx];
    [b drawInRect:rect fromRect:rect operation:NSCompositeSourceOver fraction:fraction];
    [NSGraphicsContext restoreGraphicsState];

    return [bitmap CGImage];
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/29468138

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档