核心图形效果:在模拟器上有效,但在设备中无效

问题描述 投票:0回答:1

我已经使用Core Graphics完成了简单但有效的浮雕效果。 效果很好! 但仅在模拟器中...结果如下:

第一张图片

我要做的是:-从拾取的图像中,取出具有alpha的alpha,然后用白色填充。 -我将RGB图像转换为灰度-将图像的颜色反转

然后,我调用一个自定义方法来创建带有参数的效果:

  • canvasImg:要遮罩的半透明图像
  • maskImg:我刚刚创建的图像,灰度并反转:

第二张图片

  • 不透明度:结果图像的不透明度

然后,该方法将制作一个简单的蒙版,应用阴影和不透明度,并返回一个全新的UIImage。 我不明白为什么它不能在模拟器中正常运行,也不知道设备是否正常运行。 在设备中运行时,我得到了非null的UIImage,请...帮助!

这是代码:

- (UIImage *)stampImage:(UIImage *)canvasImg withMask:(UIImage *)maskImg withOpacity:(CGFloat)opacity
{
//Creating the mask Image
CGContextRef mainViewContentContext;
CGColorSpaceRef colorSpace;
colorSpace = CGColorSpaceCreateDeviceRGB();
mainViewContentContext = CGBitmapContextCreate(NULL, maskImg.size.width, maskImg.size.height, 8, 0, colorSpace, kCGImageAlphaPremultipliedLast);
CGColorSpaceRelease(colorSpace);

if (mainViewContentContext == NULL) return NULL;

CGContextClipToMask(mainViewContentContext, CGRectMake(0, 0, maskImg.size.width, maskImg.size.height), maskImg.CGImage);
CGContextDrawImage(mainViewContentContext, CGRectMake(0, 0, maskImg.size.width, maskImg.size.height), canvasImg.CGImage);
CGContextSetAllowsAntialiasing(mainViewContentContext, true);
CGContextSetShouldAntialias(mainViewContentContext, true);
CGImageRef mainViewContentBitmapContext = CGBitmapContextCreateImage(mainViewContentContext);
CGContextRelease(mainViewContentContext);
UIImage *maskedImage = [UIImage imageWithCGImage:mainViewContentBitmapContext];
CGImageRelease(mainViewContentBitmapContext);

//Giving some Drop shadows
CGColorSpaceRef colourSpace = CGColorSpaceCreateDeviceRGB();
CGContextRef shadowContext = CGBitmapContextCreate(NULL, maskedImage.size.width + 10, maskedImage.size.height + 10,
                                                   CGImageGetBitsPerComponent(maskedImage.CGImage), 0, 
                                                   colourSpace, kCGImageAlphaPremultipliedLast);
CGColorSpaceRelease(colourSpace);
CGContextSetShadowWithColor(shadowContext, CGSizeMake(0, -1), 1, [UIColor colorWithWhite:1.0 alpha:0.3].CGColor);
CGContextSetAllowsAntialiasing(shadowContext, true);
CGContextSetShouldAntialias(shadowContext, true);
CGContextDrawImage(shadowContext, CGRectMake(0, 10, maskedImage.size.width, maskedImage.size.height), maskedImage.CGImage);
CGImageRef shadowedCGImage = CGBitmapContextCreateImage(shadowContext);
CGContextRelease(shadowContext);

UIImage *stampImg = [UIImage imageWithCGImage:shadowedCGImage];
CGImageRelease(shadowedCGImage);

return stampImg;

}

objective-c ipad ios5 core-graphics core-image
1个回答
1
投票

还要注意设备与模拟器的内存限制。 我有可以在模拟器上构建并正常运行的CG逻辑; 相同的逻辑将建立并运行,不会在设备上发出任何错误,但视觉结果并非理想。 我建议您在较小的图像上尝试使用逻辑,以验证其是否可以在设备上正常工作。 我不得不放弃一些我想出的非常酷的图像遮罩功能,因为该设备没有能力将其拉大图像。

© www.soinside.com 2019 - 2024. All rights reserved.