我正在尝试使用AdobeLabsUXMagicSelectionView,但遇到了2个问题。我想使用以下2种方法“剪切”所选区域(前景):
方法1)getForeground:andMatte:
它没有给我正确的前景。当我选择一个区域并调用getForeground:andMatte
时,我给了我前景和背景(混合)。
选择狗的脸
割狗的脸
文档说:
或者,如果您不需要处理基础位图
直接使用,并打算将结果用作CoreGraphics的输入,或
CoreImage,您可以调用:
方法2)之后,我尝试像文档一样“剪切”
extension AdobeLabsUXMagicSelectionView {
func foregroundCGImage() -> CGImage {
let w = size_t(self.image.size.width)
let h = size_t(self.image.size.height)
let data = UnsafeMutablePointer<UInt8>(malloc(4 * w * h * sizeof(UInt8)))
self.readForegroundAndMatteIntoBuffer(data)
for var i = 0; i < 4 * w * h; i += 4 {
let alpha: UInt8 = UInt8(data[i + 3]) / 255
data[i] *= alpha
data[i + 1] *= alpha
data[i + 2] *= alpha
}
let bitmapInfo = CGBitmapInfo(rawValue: CGImageAlphaInfo.NoneSkipLast.rawValue)
let ctx = CGBitmapContextCreate(data, w, h, 8, 4 * w, CGColorSpaceCreateDeviceRGB(), bitmapInfo.rawValue)
let imageRef = CGBitmapContextCreateImage(ctx)!
return imageRef
}
}
但是,它仅绘制(黑色)图像的未选择部分(背景)。
有人可以帮忙吗?我想要的是获得选定区域的最终图像。
更新:
正如@DonWoodward所说,我已经创建了这个Cattegory:
@implementation AdobeLabsUXMagicSelectionView (Foreground)
- (UIImage *)getForeground {
// show the results
// first create a UIImage of just the foreground bits per the documentation in AdobeLabsUXMagicSelectionView.h
size_t w = self.image.size.width;
size_t h = self.image.size.height;
uint8_t *data = (uint8_t *)malloc(4*w*h*sizeof(uint8_t));
[self readForegroundAndMatteIntoBuffer:data];
// Paint the non-selected portion of the image black
for (int i = 0; i < 4*w*h; i += 4) {
float alpha = (float)data[i + 3] / 255;
data[i ] *= alpha;
data[i + 1] *= alpha;
data[i + 2] *= alpha;
}
CGContextRef ctx = CGBitmapContextCreate(data, w, h, 8, 4*w, CGColorSpaceCreateDeviceRGB(), (CGBitmapInfo)kCGImageAlphaNoneSkipLast);
CGImageRef imageRef = CGBitmapContextCreateImage(ctx);
UIImage * foregroundBits = [UIImage imageWithCGImage:imageRef];
CGImageRelease(imageRef);
return foregroundBits;
}
@end
但是结果是“前景”周围有很多黑色像素。
我需要的?获取“干净的”前景(选定区域,不包含黑色像素)以覆盖UIImageView
最佳答案
在这种情况下,您要将RGB通道乘以Alpha通道,这意味着您在创建上下文时要使用kCGImageAlphaPremultipliedLast
,如下所示:
// Paint the non-selected portion of the image black
for (int i = 0; i < 4*w*h; i += 4)
{
float alpha = (float)data[i + 3] / 255;
data[i ] *= alpha;
data[i + 1] *= alpha;
data[i + 2] *= alpha;
}
CGContextRef ctx = CGBitmapContextCreate(data, w, h, 8, 4*w, CGColorSpaceCreateDeviceRGB(), (CGBitmapInfo)kCGImageAlphaPremultipliedLast);
CGImageRef imageRef = CGBitmapContextCreateImage(ctx);
UIImage * foregroundBits = [UIImage imageWithCGImage:imageRef];
CGImageRelease(imageRef);
可以在CGImage docs中找到更多信息
另外,您也可以尝试将
CGImageCreateWithMask
与以下命令的输出结合使用- (void)getForeground:(UIImage **)foregroundImage andMatte:(UIImage **)matteImage
如Quartz 2D Programming Guide中所述
UIImage *fg;
UIImage *matte;
[_magicSelectionView getForeground:&fg andMatte:&matte];
CGImageRef mattedForeground = CGImageCreateWithMask(fg.CGImage, matte.CGImage);
UIImage *foregroundBits = [UIImage imageWithCGImage:mattedForeground];
// show the results
_resultsView = [[UIImageView alloc] initWithFrame: CGRectMake(0, VIEW_Y_OFFSET, self.view.bounds.size.width, self.view.bounds.size.height-VIEW_Y_OFFSET)];
_resultsView.contentMode = UIViewContentModeScaleAspectFit;
[_resultsView setImage: foregroundBits];
[self.view addSubview: _resultsView];
如果这些都不起作用,则用于呈现结果的
UIImageViews
的配置可能存在一些问题,但是如果没有该代码,就很难说了。关于ios - 从AdobeLabsUXMagicSelectionView(Adobe Creative SDK)获取前景,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/35043434/