iOS中位图图像中的黑色边框

时间:2013-12-19 22:44:52

标签: ios objective-c uigraphicscontext

我使用以下代码创建位图图像。

    UIGraphicsBeginImageContextWithOptions(CGSizeMake(targetWidth, targetHeight), NO, 0.0);
    CGContextRef context = UIGraphicsGetCurrentContext();
    CGContextClearRect(context,CGRectMake(0, 0, targetWidth, targetHeight));


    float red ,green, blue ,alpha ;

    for (int Row = 1; Row <= targetHeight; Row++)
    {
        if (Row <= originalHeight) {
            for (int Col = 0; Col < targetWidth; iCol++)
            {
                if (Col < originalWidth) {
                    UIColor *color = [originalImage colorAtPixel:CGPointMake(Col, Row) :originalImage];
                    [color getRed:&red green:&green blue:&blue alpha:&alpha];

                    if (red == 0.0 && green == 0.0 && blue == 0.0 && alpha == 0.0) {
                        CGContextSetRGBFillColor(context, 0 , 0, 0 , 0); //set transparent pixels
                    }
                    else {
                        CGContextSetRGBFillColor(context, red , green, blue , 1);
                    }
                    CGContextFillRect(context, CGRectMake(Col,Row,1,1));

                }

            }
        }


    }
    finalImage = UIGraphicsGetImageFromCurrentImageContext();

    UIGraphicsEndImageContext();




//below code is used from another link
     - (UIColor *)colorAtPixel:(CGPoint)point :(UIImage*)image {
    // Cancel if point is outside image coordinates
    if (!CGRectContainsPoint(CGRectMake(0.0f, 0.0f, image.size.width, image.size.height), point)) {
        return nil;
    }

    NSInteger pointX = trunc(point.x);
    NSInteger pointY = trunc(point.y);

    CGImageRef cgImage = image.CGImage;
    NSUInteger width = image.size.width;
    NSUInteger height = image.size.height;
    CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
    int bytesPerPixel = 4;
    int bytesPerRow = bytesPerPixel * 1;
    NSUInteger bitsPerComponent = 8;
    unsigned char pixelData[4] = { 0, 0, 0, 0 };
    CGContextRef context = CGBitmapContextCreate(pixelData,
                                                 1,
                                                 1,
                                                 bitsPerComponent,
                                                 bytesPerRow,
                                                 colorSpace,
                                                 kCGImageAlphaPremultipliedLast | kCGBitmapByteOrder32Big);

    CGColorSpaceRelease(colorSpace);
    CGContextSetBlendMode(context, kCGBlendModeCopy);

    // Draw the pixel we are interested in onto the bitmap context
    CGContextTranslateCTM(context, -pointX, pointY-(CGFloat)height);
    CGContextDrawImage(context, CGRectMake(0.0f, 0.0f, (CGFloat)width, (CGFloat)height), cgImage);
    CGContextRelease(context);

    // Convert color values [0..255] to floats [0.0..1.0]
    CGFloat red   = (CGFloat)pixelData[0] / 255.0f;
    CGFloat green = (CGFloat)pixelData[1] / 255.0f;
    CGFloat blue  = (CGFloat)pixelData[2] / 255.0f;
    CGFloat alpha = (CGFloat)pixelData[3] / 255.0f;
    return [UIColor colorWithRed:red green:green blue:blue alpha:alpha];
}

我希望图像看起来透明。我使用CGContextSetRGBFillColor(context,0,0,0,0)将上下文的黑色背景更改为透明; //设置透明

工作正常。但它仍然在图像上留下黑色边框。我想删除它。 怎么能实现这一目标?有什么指针吗?

1 个答案:

答案 0 :(得分:0)

一种方法是制作软阈值而不是硬限制。例如,check if the luminance of a given pixel低于某个小但非零的数量。如果是这样,请将像素的alpha设置为某个插值。例如:

const double epsilon = 0.1; // or some other small value
double luminance = (red * 0.2126) + (green * 0.7152) + (blue * 0.0722);
if (luminance < epsilon) 
{
    CGContextSetRGBFillColor(context, red, green, blue, luminance / epsilon);
}
else
{
    CGContextSetRGBFillColor(context, red, green, blue, 1.0);
}