gpt4 book ai didi

swift - CALayer.compositingFilter 适用于 ObjectiveC 但不适用于 swift

转载 作者:行者123 更新时间:2023-11-28 11:05:25 29 4
gpt4 key购买 nike

下面的一些 swift 代码,它们都是干净的项目,ObjC 代码有效,swift 代码无效?据我所知相同,但在 swift 下失败了。图像显示但过滤器未在 swift 中应用,在 objC 中工作正常(OS X 10.11.3 Swift 2.2 Xcode 7.3)

import Foundation
import Cocoa

class MyView : NSView {
let layer1 = CALayer()
let layer2 = CALayer()
let filter = CIFilter(name: "CILuminosityBlendMode")

override func awakeFromNib() {

wantsLayer = true

layer!.addSublayer(layer1)

if let imageRef = NSImage(named: "Back.jpg")
{
layer1.contents = imageRef
layer1.anchorPoint = CGPointMake(0, 0)
layer1.bounds = CGRectMake(0,0, imageRef.size.width, imageRef.size.height);
}


layer!.addSublayer(layer2)
if let imageRef = NSImage(named: "Front.jpg")
{
layer2.contents = imageRef
layer2.anchorPoint = CGPointMake(0, 0)
layer2.bounds = CGRectMake(0,0, imageRef.size.width, imageRef.size.height);
}

layerUsesCoreImageFilters = true
layer2.compositingFilter = [filter!]

}

}

一些ObjC代码

MyView.h 文件

@interface MyView : NSView {

CALayer* backLayer;
CALayer* frontLayer;

}

和 MyView.m

#import <Foundation/Foundation.h>
#import "MyView.H"
#import <QuartzCore/QuartzCore.h>

@implementation MyView

- (void)awakeFromNib {

[self setWantsLayer:YES];

backLayer = [CALayer layer];
[self.layer addSublayer:backLayer];


NSImage *nsimage = [NSImage imageNamed: @"Back.jpg"];
backLayer.contents = nsimage;
backLayer.anchorPoint = CGPointMake(0, 0);
backLayer.bounds = CGRectMake(0,0, nsimage.size.width, nsimage.size.height);


frontLayer = [CALayer layer];
[self.layer addSublayer:frontLayer];

nsimage = [NSImage imageNamed: @"Front.jpg"];
frontLayer.contents = nsimage;
frontLayer.anchorPoint = CGPointMake(0, 0);
frontLayer.bounds = CGRectMake(0,0, nsimage.size.width, nsimage.size.height);

[self setLayerUsesCoreImageFilters:YES];

CIFilter* filter = [CIFilter filterWithName:@"CILuminosityBlendMode"];
frontLayer.compositingFilter = filter;
}
@end

最佳答案

在您的快速示例中,您正在传递一个数组。将最后一行更改为layer2.compositingFilter = filter!

关于swift - CALayer.compositingFilter 适用于 ObjectiveC 但不适用于 swift,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38034474/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com