2013-09-30 82 views
0

我使用alpha和白色圖像拍攝白色圖像。這可以在iOS 6中完美運行。但是,在iOS 7中,在使用此代碼之後,像素範圍的白色邊框將保留在非零字母圖像的邊緣。兩個操作系統之間會發生什麼變化,以保持像素級的白色邊框?更改iOS 6與iOS 7中的UIImage顏色不同

-(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; 
} 

回答

2

我使用下面的方法來改變圖像的顏色和我在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; 
} 

,您可以嘗試;)

+0

這主要是工作,但似乎改變了UIImageView的縱橫比。我不明白這些問題,你能解釋一下嗎? contextRect.size.height = -contextRect.size.height; contextRect.size.height - = 15; – Eric

+0

圖層被垂直翻轉,我需要再次翻轉才能正確顯示。的確,我可以刪除contextRect.size.height - = 15行。 –