我使用不同的圖像,我想包括更改顏色選項。但我不能。任何身體幫助我?我如何更改iphone sdk中的uiimage顏色xcode
3
A
回答
8
如果您想要進行圖像着色,請參見kballard/MGImageUtilities中的UIImage + Tint.m。如果您想要批量更換顏色(例如,將圖像作爲輪廓處理並將整個顏色更改爲一個平面顏色),請參見mattgemmell/MGImageUtilities中的UIImage + Tint.m。
+0
凱文@謝謝分配,現在它的工作。我可以輕鬆地更改任何圖像的顏色。 – 2011-01-14 13:24:18
+0
使用一些非常分辨率的圖像時獲取內存警告。任何想法? – Akshay 2011-12-13 13:03:54
3
完成此操作的一種方法是對圖像進行去飽和處理,然後使用所需顏色在圖像上添加色調。
去色
-(UIImage *) getImageWithUnsaturatedPixelsOfImage:(UIImage *)image {
const int RED = 1, GREEN = 2, BLUE = 3;
CGRect imageRect = CGRectMake(0, 0, image.size.width*2, image.size.height*2);
int width = imageRect.size.width, height = imageRect.size.height;
uint32_t * pixels = (uint32_t *) malloc(width*height*sizeof(uint32_t));
memset(pixels, 0, width * height * sizeof(uint32_t));
CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
CGContextRef context = CGBitmapContextCreate(pixels, width, height, 8, width * sizeof(uint32_t), colorSpace, kCGBitmapByteOrder32Little | kCGImageAlphaPremultipliedLast);
CGContextDrawImage(context, CGRectMake(0, 0, width, height), [image CGImage]);
for(int y = 0; y < height; y++) {
for(int x = 0; x < width; x++) {
uint8_t * rgbaPixel = (uint8_t *) &pixels[y*width+x];
uint32_t gray = (0.3*rgbaPixel[RED]+0.59*rgbaPixel[GREEN]+0.11*rgbaPixel[BLUE]);
rgbaPixel[RED] = gray;
rgbaPixel[GREEN] = gray;
rgbaPixel[BLUE] = gray;
}
}
CGImageRef newImage = CGBitmapContextCreateImage(context);
CGContextRelease(context);
CGColorSpaceRelease(colorSpace);
free(pixels);
UIImage * resultUIImage = [UIImage imageWithCGImage:newImage scale:2 orientation:0];
CGImageRelease(newImage);
return resultUIImage;
}
疊加隨着顏色
-(UIImage *) getImageWithTintedColor:(UIImage *)image withTint:(UIColor *)color withIntensity:(float)alpha {
CGSize size = image.size;
UIGraphicsBeginImageContextWithOptions(size, FALSE, 2);
CGContextRef context = UIGraphicsGetCurrentContext();
[image drawAtPoint:CGPointZero blendMode:kCGBlendModeNormal alpha:1.0];
CGContextSetFillColorWithColor(context, color.CGColor);
CGContextSetBlendMode(context, kCGBlendModeOverlay);
CGContextSetAlpha(context, alpha);
CGContextFillRect(UIGraphicsGetCurrentContext(), CGRectMake(CGPointZero.x, CGPointZero.y, image.size.width, image.size.height));
UIImage * tintedImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return tintedImage;
}
如何對
//For a UIImageView
yourImageView.image = [self getImageWithUnsaturatedPixelsOfImage:yourImageView.image];
yourImageView.image = [atom getImageWithTintedColor:yourImageView.image withTint:[UIColor redColor] withIntensity:0.7];
//For a UIImage
yourImage = [self getImageWithUnsaturatedPixelsOfImage:yourImage];
yourImage = [atom getImageWithTintedColor:yourImageView.image withTint:[UIColor redColor] withIntensity:0.7];
您可以將色調的顏色更改爲任何您想要的顏色。
8
其最新和最簡單的方法來做到這一點。
theImageView.image = [theImageView.image imageWithRenderingMode:UIImageRenderingModeAlwaysTemplate];
[theImageView setTintColor:[UIColor redColor]];
相關問題
- 1. 更改UIImage中的顏色
- 2. iPhone SDK反轉UIImage的顏色
- 3. 如何更改UIImage部分的顏色?
- 4. iPhone:如何更改UIImage特定像素的顏色?
- 5. iPhone SDK中如何更改BarButton的顏色項目
- 6. Iphone SDK:更改圖像顏色
- 7. 如何將UIImage中的顏色「白色」更改爲透明
- 8. 如何在iphone中將UIImage的背景更改爲不同的顏色
- 9. 如何更改titleLabel字體顏色,Xcode
- 10. 更改UIImage的像素顏色值
- 11. 更改UIImage按鈕的顏色
- 12. 如何在uiimage中更改單個像素的顏色
- 13. UIImage/CGImage改變我的像素顏色
- 14. 如何更改我的標籤Bar Corona SDK的顏色
- 15. 將uitextfield背景顏色更改爲灰色iphone sdk
- 16. 更改UIImage的顏色多於一種顏色
- 17. 將UIWebView默認顏色更改爲UIImage
- 18. 如何獲得UIImage的負顏色不改變顏色空間
- 19. 如何更改iphone中的數字顏色iphone
- 20. 如何使用xcode更改iPhone的UITextView中特定字符的顏色?
- 21. 如何在iPhone SDK中以編程方式更改鍵盤的背景顏色?
- 22. 如何在iPhone SDK中隨機更改圖像視圖的背景顏色?
- 23. iOS:將UIImage中的黑色更改爲另一種顏色
- 24. 更改面板顏色從xcode中的主顏色
- 25. Corona SDK - 我如何更改顏色文本與轉換?
- 26. 如何更改圖像上的眼睛顏色(uiimage)?
- 27. 如何更改我的導航顏色?
- 28. 如何更改我的目錄顏色?
- 29. 更改導航控制器的標題顏色! [iPhone SDK]
- 30. 如何更改SDK提供的KinectSkeletonViewer中的框架顏色v1.6
你想用一種顏色「染」圖像嗎? 「改變顏色選項」是什麼意思?如果整個圖像是白色的,則爲 – Dunkelstern 2011-01-14 12:19:26
。如果我選擇黑色按鈕,那麼整個圖像將是黑色的...等 – 2011-01-14 12:22:23