【问题标题】:IOS - Get binary mask (bitmask) from UIBeizerPathIOS - 从 UIBeizerPath 获取二进制掩码(位掩码)
【发布时间】:2012-08-26 20:34:34
【问题描述】:

我正在构建一个 IOS 应用程序。 在我的应用程序中,我有一个用户可以手动裁剪的图像。 用户裁剪图像后,我希望保存掩码信息 以二进制形式。

我需要它是一个二进制图像,而不仅仅是一个 NSCoding 形式, 因为我需要在其他平台上使用掩码信息 (不仅仅是 IOS)。

如何将 UIBezierPath 转换为二进制掩码数组。

【问题讨论】:

    标签: ios mask uibezierpath


    【解决方案1】:

    首先,您必须将您的 UIBeizerPath 栅格化(描边/填充)到您的图像上下文,然后您可以使用其栅格数据进行操作。这是一个如何栅格化 UILabel 的示例:

    UILabel* label = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, siz.w, siz.h)];
    label.text = [[NSString alloc] ... ];
    label.font = [UIFont boldSystemFontOfSize: ... ];
    
    UIGraphicsBeginImageContext(label.frame.size);
    [label.layer renderInContext:UIGraphicsGetCurrentContext()];
    UIImage* layerImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    
    // Get Image size
    GLuint width = CGImageGetWidth(layerImage.CGImage);
    GLuint height = CGImageGetHeight(layerImage.CGImage);
    CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
    
    // Allocate memory for image
    void *imageData = malloc( height * width * 4 );
    CGContextRef imgcontext = CGBitmapContextCreate(
                                                    imageData, width, height, 8, 4 * width, colorSpace,
                                                    kCGImageAlphaPremultipliedLast | kCGBitmapByteOrder32Little );
    CGColorSpaceRelease( colorSpace );
    CGContextClearRect( imgcontext,
                       CGRectMake( 0, 0, width, height ) );
    CGContextTranslateCTM( imgcontext, 0, height - height );
    CGContextDrawImage( imgcontext,
                       CGRectMake( 0, 0, width, height ), layerImage.CGImage );
    
    // Create image
    
    [.. here you can do with imageData whatever you want ..]
    
    image.InitImage(imageData, width * height * 4, width, height, iResource_Image::ImgFormat_RGBA32);
    
    
    
    // Release context
    CGContextRelease(imgcontext);
    free(imageData);
    [label release];
    

    【讨论】:

      猜你喜欢
      • 2020-04-13
      • 1970-01-01
      • 2011-11-30
      • 2016-01-10
      • 1970-01-01
      • 1970-01-01
      • 2018-07-21
      • 2015-04-15
      • 2013-08-09
      相关资源
      最近更新 更多