iphone - 在 iOS 6 和 iOS 7 中更改不同的 UIImage 颜色

标签 iphone ios uiimageview core-graphics ios7

我拍摄一张带有 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;
}

最佳答案

我正在使用以下方法更改图像的颜色,并且在 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;
}

你可以试试;)

关于iphone - 在 iOS 6 和 iOS 7 中更改不同的 UIImage 颜色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19103603/

相关文章:

iphone - 非常简单的自定义 UIView,drawRect 没有被调用

ios - 使用 Swift 的 AppDelegate 中的 UINavigationBar setBackgroundImage

ios - 轻松异步图片下载 UIImage View

ios - swift UIActivityView stopAnimating() 不工作

iphone - 当 "back"进入没有导航的 View 时,是否会触发任何函数

iphone - 如何在 Storyboard中连接 UIPickerView 数据源和委托(delegate)

ios - 在屏幕上的 3 个特定位置生成敌人 Swift SpriteKit IOS

ios - Google Vision 是否有面部识别的 Swift 文档

iphone - 调用 UIViewController 的另一个 View 不起作用