1

在成功地遵循了Ray Wenderlich 网站上的教程后,我决定尝试并操纵我学到的知识并将其应用于 CITransition,尤其是 CIDissolveTransition。但是,即使在交叉引用我从 Apple 自己的过滤器文档中学到的东西之后,我似乎也无法让图像出现(我得说,这些文档已经过时并且很难在他们自己复杂的示例之外进行调整)。这是我到目前为止所拥有的:

NSURL *url = [NSURL fileURLWithPath:imagePath];
NSURL *url2 = [NSURL fileURLWithPath:image2Path];
CIContext *context;
CIFilter *filter;
CIImage *beginImage, *targetImage;
UIImageView *mainImage = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 200, 200)];
[self addSubview:mainImage];

beginImage = [CIImage imageWithContentsOfURL:url];
targetImage = [CIImage imageWithContentsOfURL:url2];

if(context == nil)
{
  NSLog(@"Creating Context");
  context = [CIContext contextWithOptions:nil];
}
if(filter == nil) {
  NSLog(@"Creating filter");
  filter = [CIFilter filterWithName:@"CIDissolveTransition" keysAndValues:@"inputImage",   beginImage, @"inputTargetImage", targetImage, @"inputTime", [NSNumber numberWithFloat:0.5], nil];
}

CIImage *outputImage = [filter outputImage];

CGImageRef cgimg = [context createCGImage:outputImage fromRect:[outputImage extent]];
UIImage *newImg = [UIImage imageWithCGImage:cgimg];

[mainImage setImage:newImg];

我得到的只是应该显示 mainImage 的黑屏。最终目标是能够使用滑块(我只是假设它会起作用)循环通过具有这种溶解效果的一系列图像,但我什至无法显示这个静态图像。任何帮助将不胜感激。

4

3 回答 3

6

您对 filterWithName:keysAndValues: 的调用返回 nil,因为 iOS 不知道如何制作 CIDossolveTransition 过滤器。Core Image for iOS 不支持 Core image for Mac OS 支持的所有过滤器。看到这个答案

您可以阅读Apple 的核心图像过滤器参考并查看 iOS 支持哪些过滤器,但坏消息是转场并没有适用于 iOS 5。iOS 5 中唯一可用的处理合成的过滤器是 CISourceOverCompositing,它可能对你来说不是很有趣。

于 2012-04-13T00:38:19.857 回答
1

The Apple WWDC 2014 session "Advances in Core Image" listed 115 CIFilters available in iOS. The CIDissolveTransition is listed as supported as of 2014. See session 514 https://developer.apple.com/videos/play/wwdc2014/514/ Try it again !

于 2016-03-16T16:34:41.813 回答
0

正如 Jay 在他的回答中提到的,iOS 不支持过渡。但是,我找到并编辑了一个脚本,该脚本抓取两个图像的像素值并根据百分比返回两者的交叉。这是方法:

- (UIImage*)morphImage:(UIImage*)img1
             withImage:(UIImage*)img2
             withValue:(float)val
{  
CGImageRef sourceImage = img1.CGImage;
CGImageRef sourceImage2 = img2.CGImage;

CFDataRef theData;
theData = CGDataProviderCopyData(CGImageGetDataProvider(sourceImage));
CFDataRef theData2;
theData2 = CGDataProviderCopyData(CGImageGetDataProvider(sourceImage2));

UInt8 *pixelData = (UInt8 *) CFDataGetBytePtr(theData);
UInt8 *pixelData2 = (UInt8 *) CFDataGetBytePtr(theData2);

int dataLength = CFDataGetLength(theData);

//color byte increments
int red = 0;
int green = 1;
int blue = 2;
//temporary rgb values for both images
int r1, g1, b1, r2, g2, b2;
//fake slider value
for (int index = 0; index < dataLength; index += 4) {
r1 = pixelData[index + red];
g1 = pixelData[index + green];
b1 = pixelData[index + blue];
r2 = pixelData2[index + red];
g2 = pixelData2[index + green];
b2 = pixelData2[index + blue];

pixelData[index + red] += (r2 - r1) * val;
pixelData[index + green] += (g2 - g1) * val;
pixelData[index + blue] += (b2 - b1) * val;
}

CGContextRef context;
context = CGBitmapContextCreate(pixelData,
                              CGImageGetWidth(sourceImage),
                              CGImageGetHeight(sourceImage),
                              8,
                              CGImageGetBytesPerRow(sourceImage),
                              CGImageGetColorSpace(sourceImage),
                              kCGImageAlphaPremultipliedLast);

CGImageRef newCGImage = CGBitmapContextCreateImage(context);
UIImage *newImage = [UIImage imageWithCGImage:newCGImage];

CGContextRelease(context);
CFRelease(theData);
CGImageRelease(newCGImage);

return newImage;

}

我没有在设备上测试过它,但是当使用滑块循环浏览 6 到 10 张 200x200 图像时,它在模拟器上运行得非常流畅。我想通过预先提取图像像素数据可能会更有效。

于 2012-04-13T18:28:43.007 回答