English 中文(简体)
从CIFilter“CIRadialGradient”获得无核空间测量。
原标题:Get NSImage from CIFilter "CIRadialGradient"

我必须生成一个图像 < a href=" "http:// developmenter.apple.com/library/mac/#document/Graphicsimage/Reference/CoreImageFilterReference/Reference/Reference/ reference.html#//apple_ref/doc/filter/ci/CIRadialGradient" rel="nofollown norefererr", 和这里在CIRadialGradient苹果文档中找到的 :

"https://i.sstatic.net/UoH89.gif" alt="CIRadialGradient"/> https://i.sstatic.net/UoH89.gif" alt="CIRadial Gradient"/>

我尝试了很多方法,最后却说:

+(NSImage*)radialGradientWithR0:(float)r0 r1:(float)r1 c0:(NSColor*)c0 c1:(NSColor*)c1 imageDimensionPx:(int)imageDimension
{
    CIColor* cc0 = [[CIColor alloc]initWithColor:c0];
    CIColor* cc1 = [[CIColor alloc]initWithColor:c1];
    CIVector* civ = [CIVector vectorWithX:imageDimension/2 Y:imageDimension/2];

    CIFilter* filter = [CIFilter filterWithName:@"CIRadialGradient"];
    [filter setDefaults];
    [filter setValue:civ forKey:@"inputCenter"];
    [filter setValue:[NSNumber numberWithFloat:r0] forKey:@"inputRadius0"];
    [filter setValue:[NSNumber numberWithFloat:r0] forKey:@"inputRadius1"];
    [filter setValue:cc0 forKey:@"inputColor0"];
    [filter setValue:cc1 forKey:@"inputColor1"];

    CIImage* outputImage = [filter valueForKey:@"outputImage"];

    NSRect outputImageRect = NSRectFromCGRect([outputImage extent]);
    NSImage* blurredImage = [[NSImage alloc]
                             initWithSize:NSMakeSize(imageDimension,  imageDimension)];

    [blurredImage lockFocus];
    [blurredImage drawAtPoint:NSZeroPoint fromRect:NSMakeRect(0, 0, imageDimension, imageDimension) operation:NSCompositeSourceOver fraction:1.0];
    [blurredImage unlockFocus];

    return blurredImage;
}

但我只能得到一个空虚的图像。

帮帮我吧

谢谢 谢谢

菲利克斯

最佳回答

您的代码中有一些错误, 阻止它工作 :

  • typo in the arguments: r1 is not used in the second call

您需要更改第二行:

 [filter setValue:[NSNumber numberWithFloat:r0] forKey:@"inputRadius0"];
 [filter setValue:[NSNumber numberWithFloat:r0] forKey:@"inputRadius1"];
  • 您不需要这个 : NSRect 输出图像矩形 = NSRectfromCGRect ([产出范围]);

  • [blurredImaage lockFocus]; 之后是 [blurredImaage drawAtpoint: 基本上意指类似的东西: 自己绘制图像。 您的意思是: [utimage drawAtpoint:.]

以下是一个方法, 做你想做的事 :

-(NSImage *) radialGradientWithR0:(float)r0 r1:(float)r1 c0:(NSColor*)c0 c1:(NSColor*)c1 imageDimensionPx:(int)imageDimension {

    CIColor* cc0 = [[CIColor alloc]initWithColor:c0];
    CIColor* cc1 = [[CIColor alloc]initWithColor:c1];
    CIVector* civ = [CIVector vectorWithX:imageDimension/2 Y:imageDimension/2];

    CIFilter* filter = [CIFilter filterWithName:@"CIRadialGradient"];

    [filter setValue:civ forKey:@"inputCenter"];
    [filter setValue:[NSNumber numberWithFloat:r0] forKey:@"inputRadius0"];
    [filter setValue:[NSNumber numberWithFloat:r1] forKey:@"inputRadius1"];
    [filter setValue:cc0 forKey:@"inputColor0"];
    [filter setValue:cc1 forKey:@"inputColor1"];

    NSImage * res = [[NSImage alloc] initWithSize:NSMakeSize(imageDimension, imageDimension)];
    [res lockFocus];
    [[filter valueForKey:@"outputImage"]
     drawAtPoint:NSZeroPoint 
     fromRect:NSMakeRect(0, 0, imageDimension, imageDimension)                   
     operation:NSCompositeDestinationAtop  fraction:1.0];
    [res unlockFocus];
    return res;
}

"https://i.sstatic.net/Xr6Wt.png" alt="NSImage Wells 显示半径梯度"/ >

问题回答

暂无回答




相关问题
2 mysql instances in MAC

i recently switched to mac. first and foremost i installed xampp. then for django-python-mysql connectivity, i "somehow" ended up installing a seperate MySQL. now the seperate mysql installation is ...

Iterating over string/strlen with umlauted characters

This is a follow-up to my previous question . I succeeded in implementing the algorithm for checking umlauted characters. The next problem comes from iterating over all characters in a string. I do ...

Controlling OSX windows

I m trying to control windows of a foreign OSX applications from my application. I d like to 1. move the windows on the screen 2. resize the windows on the screen 3. change the currently active window ...

Switching J2SE versions on Mac OS (SnowLeopard)

My current JDK on Mac OS (10.6) is set to 1.6 and I d like to switch to 1.5. A listing of /System/Library/Frameworks/JavaVM.framework/Versions/ shows: lrwxr-xr-x 1 root wheel 10 Nov 3 18:34 ...

Scrolling inside Vim in Mac s Terminal

I ve been googling around trying to figure out if it s possible to use my mouse wheel to scroll while inside Vim in Mac s Terminal, with no luck. It seems as if only X11 or iTerm support this. Before ...

export to MP3 from quicktime API

A question for Apple,QT programmers. Would like to know if it s possible to export a Movie object to MP3 using the QuickTime API. Preferably the ConvertMovieToFile function. I ve looked at ...

热门标签