【问题标题】:Get foreground from AdobeLabsUXMagicSelectionView (Adobe Creative SDK)从 Adob​​eLabsUXMagicSelectionView (Adobe Creative SDK) 获取前景
【发布时间】:2016-01-27 16:55:39
【问题描述】:

我正在尝试使用AdobeLabsUXMagicSelectionView,但遇到了两个问题。我想使用这两种方法“剪切”选定区域(前景):

方法 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 上

【问题讨论】:

    标签: ios uiimage core-graphics cgimage adobecreativesdk


    【解决方案1】:

    我认为问题在于您的比例因子是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];
    

    【讨论】:

      【解决方案2】:

      在这种情况下,您将 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 的配置可能存在一些问题,但如果没有该代码,就很难说。

      【讨论】:

        猜你喜欢
        • 2017-11-21
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-09-08
        • 1970-01-01
        • 2015-05-10
        相关资源
        最近更新 更多