1

我正在尝试使用Adob​​eLabsUXMagicSelectionView,但遇到了 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 所说,我创建了这个类别:

@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 上

4

2 回答 2

3

我认为问题在于您的比例因子是类型uint8_t,因此您将比例因子剪裁为零。让它成为一个浮动,它应该工作。这是来自 MagicPuppy(在目标 c 中)的代码:

    // show the results
    // first create a UIImage of just the foreground bits per the documentation in AdobeLabsUXMagicSelectionView.h
    size_t w = _magicSelectionView.image.size.width;
    size_t h = _magicSelectionView.image.size.height;

    uint8_t *data = (uint8_t *)malloc(4*w*h*sizeof(uint8_t));
    [_magicSelectionView 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);

    // 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];
于 2016-02-02T18:30:30.880 回答
1

在这种情况下,您将 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 文档中找到

此外,您还可以尝试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,但是如果没有该代码,就很难说。

于 2016-03-17T08:05:42.723 回答