2011-02-28 70 views
5

2 điều tôi muốn làm, có liên quan:Làm cách nào để tô màu ảnh/hiển thị màu?

  1. Hiển thị khối bất kỳ màu nào. Vì vậy, tôi có thể thay đổi màu đó thành một thứ khác bất cứ lúc nào.
  2. Tô màu UIImage thành màu khác. Một lớp phủ màu với alpha bị tắt có thể hoạt động ở đây, nhưng nói đó là một hình ảnh có nền trong suốt và không chiếm hết hình vuông của hình ảnh.

Bất kỳ ý tưởng nào?

Trả lời

10

Đầu tiên dễ dàng. Tạo UIView mới và đặt màu nền thành bất kỳ màu nào bạn muốn.

Thứ hai khó hơn. Như bạn đã đề cập, bạn có thể đặt chế độ xem mới lên trên cùng với độ trong suốt bị giảm xuống, nhưng để làm cho nó ở cùng một vị trí, bạn sẽ muốn sử dụng mặt nạ. Một cái gì đó như thế này:

UIImage *myImage = [UIImage imageNamed:@"foo.png"]; 

UIImageView *originalImageView = [[UIImageView alloc] initWithImage:myImage]; 
[originalImageView setFrame:CGRectMake(0.0f, 0.0f, 100.0f, 100.0f)]; 
[parentView addSubview:originalImageView]; 

UIView *overlay = [[UIView alloc] initWithFrame:[originalImageView frame]]; 

UIImageView *maskImageView = [[UIImageView alloc] initWithImage:myImage]; 
[maskImageView setFrame:[overlay bounds]]; 

[[overlay layer] setMask:[maskImageView layer]]; 

[overlay setBackgroundColor:[UIColor redColor]]; 

[parentView addSubview:overlay]; 

Hãy nhớ rằng bạn sẽ phải #import <QuartzCore/QuartzCore.h> trong tệp triển khai.

+0

Tôi gặp khó khăn khi tham gia hori zontal trung tâm để làm việc. Lúc đầu, tôi đã thêm lớp phủ như là một subview của một UITableViewCell, và trung tâm đã bị mất khi các tế bào thay đổi kích cỡ. Tôi đã sửa lỗi này bằng cách thêm lớp phủ dưới dạng phần phụ của trường UILabel trong ô, thay vì chính UITableViewCell. – bneely

+1

Bạn cũng có thể thử thêm nó vào 'contentView' của ô xem bảng. –

2

Cách dễ dàng để đạt được 1 là tạo UILabel hoặc thậm chí là UIView và thay đổi backgroundColor theo ý muốn.

Có một cách để nhân màu thay vì chỉ che phủ chúng và điều đó sẽ hoạt động đối với 2. Xem this tutorial.

11

Một lựa chọn khác, sẽ được sử dụng phương pháp loại trên UIImage như thế này ...

// Tint the image, default to half transparency if given an opaque colour. 
- (UIImage *)imageWithTint:(UIColor *)tintColor { 
    CGFloat white, alpha; 
    [tintColor getWhite:&white alpha:&alpha]; 
    return [self imageWithTint:tintColor alpha:(alpha == 1.0 ? 0.5f : alpha)]; 
} 

// Tint the image 
- (UIImage *)imageWithTint:(UIColor *)tintColor alpha:(CGFloat)alpha { 

    // Begin drawing 
    CGRect aRect = CGRectMake(0.f, 0.f, self.size.width, self.size.height); 
    UIGraphicsBeginImageContext(aRect.size); 

    // Get the graphic context 
    CGContextRef c = UIGraphicsGetCurrentContext(); 

    // Converting a UIImage to a CGImage flips the image, 
    // so apply a upside-down translation 
    CGContextTranslateCTM(c, 0, self.size.height); 
    CGContextScaleCTM(c, 1.0, -1.0); 

    // Draw the image 
    [self drawInRect:aRect]; 

    // Set the fill color space 
    CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB(); 
    CGContextSetFillColorSpace(c, colorSpace); 

    // Set the mask to only tint non-transparent pixels 
    CGContextClipToMask(c, aRect, self.CGImage); 

    // Set the fill color 
    CGContextSetFillColorWithColor(c, [tintColor colorWithAlphaComponent:alpha].CGColor); 

    UIRectFillUsingBlendMode(aRect, kCGBlendModeColor); 

    UIImage *img = UIGraphicsGetImageFromCurrentImageContext(); 
    UIGraphicsEndImageContext();  

    // Release memory 
    CGColorSpaceRelease(colorSpace); 

    return img; 
} 
+0

cũng tốt, nhưng các điểm ảnh trong suốt cũng có màu sắc quá – zxcat

+2

1) Để khắc phục "các pixel trong suốt cũng được tô màu", hãy thêm 'CGContextClipToMask (c, aRect, self.CGImage);' trước 'UIRectFillUsingBlendMode'. 2) Để áp dụng màu đúng, bảo quản các giá trị hình ảnh, hãy sử dụng kCGBlendModeColor. – Wienke

+1

Điều này làm cho hình ảnh lộn ngược. Sau khi nhận được bối cảnh, các dòng sau cần phải được thêm vào: \t 'CGContextTranslateCTM (c, 0, self.size.height); CGContextScaleCTM (c, 1.0, -1.0); ' – chitza

-1

Hãy thử điều này

- (void)viewDidLoad 
    { 
     [super viewDidLoad]; 
     UIView* maskedView = [self filledViewForPNG:[UIImage imageNamed:@"mask_effect.png"] 
               mask:[UIImage imageNamed:@"mask_image.png"] 
              maskColor:[UIColor colorWithRed:.6 green:.2 blue:.7 alpha:1]]; 


     [self.view addSubview:maskedView]; 

    } 

    -(UIView*)filledViewForPNG:(UIImage*)image mask:(UIImage*)maskImage maskColor:(UIColor*)maskColor 
    { 
     UIImageView *pngImageView = [[UIImageView alloc] initWithImage:image]; 
     UIImageView *maskImageView = [[UIImageView alloc] initWithImage:maskImage]; 

     CGRect bounds; 
     if (image) { 
      bounds = pngImageView.bounds; 
     } 
     else 
     { 
      bounds = maskImageView.bounds; 
     } 
     UIView* parentView = [[UIView alloc]initWithFrame:bounds]; 
     [parentView setAutoresizesSubviews:YES]; 
     [parentView setClipsToBounds:YES]; 

     UIView *overlay = [[UIView alloc] initWithFrame:bounds]; 
     [[overlay layer] setMask:[maskImageView layer]]; 
     [overlay setBackgroundColor:maskColor]; 

     [parentView addSubview:overlay]; 
     [parentView addSubview:pngImageView]; 
     return parentView; 
    } 
+0

Vui lòng giải thích câu trả lời của bạn. – hims056

4

Đây là một cách khác để thực hiện hình ảnh pha màu, đặc biệt là nếu bạn đã sử dụng QuartzCore cho một thứ khác.

nhập QuartzCore:

#import <QuartzCore/QuartzCore.h>  

Tạo CALayer trong suốt và thêm nó như là một lớp con cho hình ảnh mà bạn muốn màu:

CALayer *sublayer = [CALayer layer]; 
[sublayer setBackgroundColor:[UIColor whiteColor].CGColor]; 
[sublayer setOpacity:0.3]; 
[sublayer setFrame:toBeTintedImage.frame]; 
[toBeTintedImage.layer addSublayer:sublayer]; 

Thêm QuartzCore để dự án của bạn danh sách khung (nếu nó isn' t đã có), nếu không bạn sẽ nhận được lỗi trình biên dịch như sau:

Undefined symbols for architecture i386: "_OBJC_CLASS_$_CALayer" 
Các vấn đề liên quan