[英]Change Pixel colour values of UIImage
我想更改UIImage中所有像素的顏色。 這段代碼成功地更改了一個點的顏色,但是當我為整個圖像循環運行它時,我的圖像消失了。 首先,我讀取像素顏色值,然后將更改應用於該像素的顏色。 同樣,循環僅針對我的0值運行。 下面是代碼
UIImage *grayImage=[UIImage imageWithCGImage:source.CGImage];
for(int i=0;i<imageToTest.frame.size.width;i++){
for(int j=0;j<imageToTest.frame.size.height;j++){
NSLog(@"%d",i);
CGPoint point=CGPointMake((CGFloat) i,(CGFloat) j);
if (!CGRectContainsPoint(CGRectMake(0.0f, 0.0f, source.size.width, source.size.height), point)) {
return nil;
}
NSInteger pointX = trunc(point.x);
NSInteger pointY = trunc(point.y);
CGImageRef cgImage = source.CGImage;
NSUInteger width = CGImageGetWidth(cgImage);
NSUInteger height = CGImageGetHeight(cgImage);
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);
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;
UIColor *currentPixelColor;
CGFloat Answer=sliderValue/10;
red=red*Answer;
green=green*Answer;
blue=blue*Answer;
alpha=alpha*Answer;
currentPixelColor=[UIColor colorWithRed:red green:green blue:blue alpha:alpha];
int width2 = source.size.width;
int height2 = source.size.height;
CGColorSpaceRef colorSpace2 = CGColorSpaceCreateDeviceRGB();
CGContextRef context2 = CGBitmapContextCreate (nil,
width2,
height2,
8,
0,
colorSpace2,
kCGImageAlphaPremultipliedFirst);
CGColorSpaceRelease(colorSpace2);
if (context2 == NULL) {
return nil;
}
CGContextDrawImage(context2,
CGRectMake(0, 0, width2, height2), grayImage.CGImage);
CGContextSetFillColorWithColor(context2, currentPixelColor.CGColor);
CGContextFillRect(context2, CGRectMake(point.x, point.y, 1, 1));
grayImage = [UIImage imageWithCGImage:CGBitmapContextCreateImage(context2)];
CGContextRelease(context2);
}
}
return grayImage;
}
我懷疑您正在遇到這種情況並返回nil(因此您的圖像消失了):
if (!CGRectContainsPoint(CGRectMake(0.0f, 0.0f, source.size.width, source.size.height), point)) {
return nil;
}
我認為您的迭代應該在source.size.width和source.size.height之上,而不是包含視圖的維度。
附帶一提,您可能正在迭代到很高的水平。 僅在CGContextRef上創建並應用所有更改可能(並且肯定會更有效)。 老實說,我沒有足夠仔細地研究您的算法,但是您應該嘗試將不需要反復重申的所有內容都從循環中移出。
您應該考慮使用Core Image濾鏡進行顏色調整。 它們非常快,而且非常易於使用。 埃里卡·薩頓(Erica Sadun)的書《 iOS 5開發人員的食譜》包括一章關於使用CI的章節,該章非常清晰易用。 它還包括一個針對OS錯誤的變通辦法,該錯誤可防止您將修改后的圖像從Core Image移回到UIImage。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.