【问题标题】:Change UIImage Color different in iOS 6 vs. iOS 7在 iOS 6 和 iOS 7 中更改 UIImage 颜色不同
【发布时间】:2013-09-30 20:51:39
【问题描述】:

我拍摄了一张带有 alpha 的白色图像并为其着色。这与 iOS 6 中的以下代码完美配合。但是,在 iOS 7 中,使用此代码后,非零 alpha'd 图像的边缘周围仍保留一个像素宽的白色边框。两个操作系统之间有什么变化会保持像素宽的白色边框?

-(void)changeImageColorToColor: (UIColor *)color

     CGFloat redC = 0.0, greenC = 0.0, blueC = 0.0;

     const CGFloat *components = CGColorGetComponents(color.CGColor);
     redC = components[0];
     greenC = components[1];
     blueC = components[2];

    CGImageRef sourceImage = self.addedImageView.whiteColorVersion;

    CFDataRef theData;

    theData = CGDataProviderCopyData(CGImageGetDataProvider(sourceImage));

    CFMutableDataRef mutableData = CFDataCreateMutableCopy(0, 0, theData);

    UInt8 *pixelData = (UInt8 *) CFDataGetBytePtr(mutableData);

    int dataLength = CFDataGetLength(mutableData);


    for (int index = 0; index < dataLength; index += 4) {
        pixelData[index + 0] = pixelData[index + 0] * redC;
        pixelData[index + 1] = pixelData[index + 1] * greenC;
        pixelData[index + 2] = pixelData[index + 2] * blueC;
    }   

    CGContextRef context;
    context = CGBitmapContextCreate(pixelData,
                                    CGImageGetWidth(sourceImage),
                                    CGImageGetHeight(sourceImage),
                                    8,
                                    CGImageGetBytesPerRow(sourceImage),
                                    CGImageGetColorSpace(sourceImage),
                                    kCGImageAlphaPremultipliedLast);

    CGImageRef newCGImage = CGBitmapContextCreateImage(context);
    UIImage *newImage = [UIImage imageWithCGImage:newCGImage];

    CGContextRelease(context);
    CFRelease(theData);
    CFRelease(mutableData);
    CGImageRelease(newCGImage);

    self.addedImageView.image = newImage;
}

【问题讨论】:

    标签: iphone ios uiimageview core-graphics ios7


    【解决方案1】:

    我正在使用以下方法更改图像的颜色,并且在 iOS7 或 iOS6 中没有任何问题:

    - (UIImage *) tintImageWithColor: (UIColor *) color
    {    
        CGRect contextRect;
        contextRect.origin.x = 0.0f;
        contextRect.origin.y = 0.0f;
        contextRect.size = [self size];
        // Retrieve source image and begin image context
        CGSize itemImageSize = [self size];
        CGPoint itemImagePosition;
        itemImagePosition.x = ceilf((contextRect.size.width - itemImageSize.width) / 2);
        itemImagePosition.y = ceilf((contextRect.size.height - itemImageSize.height) );
    
        if ([[UIScreen mainScreen] respondsToSelector:@selector(scale)])
            UIGraphicsBeginImageContextWithOptions(contextRect.size, NO, [[UIScreen mainScreen] scale]); //Retina support
        else
            UIGraphicsBeginImageContext(contextRect.size);
    
        CGContextRef c = UIGraphicsGetCurrentContext();
        // Setup shadow
        // Setup transparency layer and clip to mask
        CGContextBeginTransparencyLayer(c, NULL);
        CGContextScaleCTM(c, 1.0, -1.0);
        CGContextClipToMask(c, CGRectMake(itemImagePosition.x, -itemImagePosition.y, itemImageSize.width, -itemImageSize.height), [self CGImage]);
    
        // Fill and end the transparency layer
        color = [color colorWithAlphaComponent:1.0];
    
        CGContextSetFillColorWithColor(c, color.CGColor);
    
        contextRect.size.height = -contextRect.size.height;
        CGContextFillRect(c, contextRect);
        CGContextEndTransparencyLayer(c);
    
        UIImage *img = UIGraphicsGetImageFromCurrentImageContext();
        UIGraphicsEndImageContext();
    
        return img;
    }
    

    你可以试试;)

    【讨论】:

    • 这主要是可行的,但似乎改变了 UIImageView 的纵横比。我不明白这些行,你能解释一下吗? contextRect.size.height = -contextRect.size.height; contextRect.size.height -= 15;
    • 图层是垂直翻转绘制的,我需要再次翻转才能正确显示。确实可以删除 contextRect.size.height -= 15 行。
    猜你喜欢
    • 2012-09-05
    • 2016-08-11
    • 1970-01-01
    • 2013-08-13
    • 2013-09-28
    • 2014-10-18
    • 1970-01-01
    • 2014-04-20
    • 2023-03-18
    相关资源
    最近更新 更多