我真的坚持用一定的色调着色UIImage。 UIImage位于自定义TableView单元格中(如果这有任何区别)。
查看图片,UIImage已被0.5不透明度黄色取代,原始图像已被丢弃。我想要对现有图像着色,而不是完全覆盖图像。 (p.s:图片中的数字是图像顶部的标签,它们是无关的)。
我意识到stackoverflow上有类似的线程,我必须尝试所有的代码:-)没有什么工作。我不希望任何人发布任何代码,只是推动正确的方向!
以下代码说明:
有没有人有想法,推动正确的方向?
更新
MasterViewController.m
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
ArticleCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell" forIndexPath:indexPath];
Article *article = self.articles[indexPath.row];
Utils *util = [[Utils alloc] init];
// Populate cell components
UIImage *myImage = [UIImage imageWithData:article.post_image_thumbnail_data];
// Set image tint
UIColor *color = [UIColor colorWithRed:(160/15.0) green:(97/15.0) blue:(5/55.0) alpha:0.5];
myImage = [util tintedImageWithColor:(UIImage *) myImage:(UIColor *) color blendingMode:kCGBlendModeDestinationIn highQuality:YES];
cell.imageView.image = myImage;
return cell;
}
Utils.m
- (UIImage *)tintedImageWithColor:(UIImage*)image : (UIColor *)tintColor blendingMode:(CGBlendMode)blendMode highQuality:(BOOL) yerOrNo;
{
CGSize size = CGSizeMake(70, 70);
UIGraphicsBeginImageContextWithOptions(size, NO, 0.0f);
if (yerOrNo) {
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextSetShouldAntialias(context, true);
CGContextSetAllowsAntialiasing(context, true);
CGContextSetInterpolationQuality(context, kCGInterpolationHigh);
}
[tintColor setFill];
CGRect bounds = CGRectMake(0, 0, size.width, size.height);
UIRectFill(bounds);
[image drawInRect:bounds blendMode:blendMode alpha:1.0f];
if (blendMode != kCGBlendModeDestinationIn)
[image drawInRect:bounds blendMode:kCGBlendModeDestinationIn alpha:0.4];
UIImage *tintedImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return tintedImage;
}
Utils.h
- (UIImage *)tintedImageWithColor:(UIImage*)image : (UIColor *)tintColor blendingMode:(CGBlendMode)blendMode highQuality:(BOOL) yerOrNo;
答案 0 :(得分:1)
我在我的一个项目中发现了这段代码,我不知道确切的来源,但它应该有效,使用kCGBlendModeDestinationIn
:
- (UIImage *)tintedImageWithColor:(UIColor *)tintColor blendingMode:(CGBlendMode)blendMode highQuality:(BOOL) yerOrNo;
{
UIGraphicsBeginImageContextWithOptions(self.size, NO, 0.0f);
if (yerOrNo) {
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextSetShouldAntialias(context, true);
CGContextSetAllowsAntialiasing(context, true);
CGContextSetInterpolationQuality(context, kCGInterpolationHigh);
}
[tintColor setFill];
CGRect bounds = CGRectMake(0, 0, self.size.width, self.size.height);
UIRectFill(bounds);
[self drawInRect:bounds blendMode:blendMode alpha:1.0f];
if (blendMode != kCGBlendModeDestinationIn)
[self drawInRect:bounds blendMode:kCGBlendModeDestinationIn alpha:1.0];
UIImage *tintedImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return tintedImage;
}