2017-07-07 55 views
1

我想在用户触摸时更改图像视图的颜色。我不需要它来改变很多。但是,我希望它变成一片蓝色。我发现下面的代码来改变渐变,但它没有任何效果。实际上,我不需要渐变,只有色调。将不胜感激任何建议。iOS/Objective-C:更改图像视图的色调

UIView *snapshot = [[UIImageView alloc] initWithImage:image]; 
     snapshot.layer.masksToBounds = NO; 
      CAGradientLayer *gradient = [CAGradientLayer layer]; 

     gradient.colors = @[(id)[UIColor redColor].CGColor, (id)[UIColor blackColor].CGColor]; 

     [snapshot.layer insertSublayer:gradient atIndex:0]; 
+0

'someView.backgroundColor = [的UIColor blueColor];' – danh

回答

1

方法创建一个简单的和非侵入性的方式是一个半透明的覆盖添加到您的图像视图。这可以随着按钮被按下并释放而被显示和隐藏。

- (void)indicatedImageViewPressed:(UIImageView *)imageView on:(BOOL)on { 
    UIView *overlay = [imageView viewWithTag:1]; // See if already created 
    if (overlay == nil) { 
     overlay = [[UIView alloc] initWithFrame:imageView.bounds]; 
     overlay.tag = 1; // Mark view to find it next time 
     overlay.backgroundColor = [UIColor colorWithRed:0.0 green:0.0 blue:1.0 alpha:0.3]; // Your color overlay goes here 
     [imageView addSubview:overlay]; 
    } 
    overlay.hidden = !on; 
} 
+0

我改变了签名中的imageView到UIView,它像一个魅力一样工作,所以不需要将视图改变为imageview。 – user6631314

1

你可以尝试给UIImage的色调

UIImage *image = [[UIImage imageNamed:@"Your Image"] imageWithRenderingMode:UIImageRenderingModeAlwaysTemplate]; 
UIImageView *imageView = [[UIImageView alloc] initWithImage:image]; 
imageView.tintColor = [UIColor clearColor]; // Or any color really you'd like would work 
imageView.image = newImage; 

然后改变这种色调当用户使用敲击手势识别触摸图像

+0

我开始与一个UIView,不是图像。我还能用这个吗?另外通过newImage你的意思是第一行的图像? – user6631314

+0

你从一个UIView开始,但是你将它分配为一个UIImageView。 如果你真的使用UIView,你将如何显示图像? – Prientus

+0

作为其他代码的一部分,视图被移动了很多。当我尝试时,snapshot.image在快照上找不到任何属性图像。我不愿意将快照更改为实际的UIImage,因为在其他代码中发生了各种各样的事情。 – user6631314

0

更改快照UIImageView

解决方案1:

UIImage *image = [[UIImage imageNamed:@"Your Image"] imageWithRenderingMode:UIImageRenderingModeAlwaysTemplate]; 
UIImageView *snapshotView = [[UIImageView alloc] initWithImage:image]; 
imageView.tintColor = [UIColor clearColor]; 

解决方案2: 继续与UIView的用于获取图像从UIView

#import "QuartzCore/QuartzCore.h" 

- (UIImage *) imageFromView:(UIView *)view 
{ 
    UIGraphicsBeginImageContext(view.frame.size); 
    [[view layer] renderInContext:UIGraphicsGetCurrentContext()]; 
    UIImage *screenshot = UIGraphicsGetImageFromCurrentImageContext(); 
    UIGraphicsEndImageContext(); 
    return screenshot; 
}