2015-01-10 59 views
1

我需要填寫不同的色彩搭配面膜我從我們的設計師在Photoshop文件中得到了編程圖像: http://image.openlan.ru/images/83339350420766181806.png圖像屏蔽之後保持正確的色彩

我幾乎完成這只是細節我錯過:

UIImage *mask = [UIImage imageNamed:@"mask.png"]; 
UIImage *image = [UIImage imageNamed:@"vehicle.png"]; 

mask = [image maskImage:image withMask:mask]; 
image = [image coloredImageWithColor:[UIColor blueColor]]; 
image = [image drawImage:mask inImage:image]; 

如果方法實現:

- (UIImage *) maskImage:(UIImage *)image withMask:(UIImage *)maskImage 
{ 
    CGImageRef maskRef = maskImage.CGImage; 

    CGImageRef mask = CGImageMaskCreate(CGImageGetWidth(maskRef), 
             CGImageGetHeight(maskRef), 
             CGImageGetBitsPerComponent(maskRef), 
             CGImageGetBitsPerPixel(maskRef), 
             CGImageGetBytesPerRow(maskRef), 
             CGImageGetDataProvider(maskRef), NULL, false); 

    CGImageRef masked = CGImageCreateWithMask([image CGImage], mask); 

    UIImage *im = [UIImage imageWithCGImage:masked]; 

    CGImageRelease(masked); 
    CGImageRelease(maskRef); 
    CGImageRelease(mask); 

    return im; 
} 

- (UIImage *) coloredImageWithColor:(UIColor *)color 
{ 
    UIGraphicsBeginImageContext(self.size); 

    CGContextRef context = UIGraphicsGetCurrentContext(); 

    CGContextTranslateCTM(context, 0, self.size.height); 
    CGContextScaleCTM(context, 1.0, -1.0); 

    CGContextSetBlendMode(context, kCGBlendModeDarken); 
    CGRect rect = (CGRect){ CGPointZero, self.size }; 
    CGContextDrawImage(context, rect, self.CGImage); 
    CGContextClipToMask(context, rect, self.CGImage); 

    [color set]; 
    CGContextFillRect(context, rect); 

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

    return coloredImage; 
} 

- (UIImage *) drawImage:(UIImage *)fgImage inImage:(UIImage *)bgImage 
{ 
    UIGraphicsBeginImageContextWithOptions(bgImage.size, FALSE, 0.0); 
    [bgImage drawInRect:(CGRect){ CGPointZero, bgImage.size }]; 
    [fgImage drawInRect:(CGRect){ CGPointZero, fgImage.size }]; 
    UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext(); 
    UIGraphicsEndImageContext(); 
    return newImage; 
} 

我現在的結果是: http://image.openlan.ru/images/34429534616655410787.png

我不認爲如果我使用漸變,我會看到正確的結果。我現在不喜歡的是某些圖像區域缺乏色調。我在這裏做錯了什麼?

回答

0

受此啓發post我最終實現了我的任務。

這是一個來源國形象:

enter image description here

然後我畫這個像用光罩不填特殊區域:

enter image description here

下面主要方法繪製這樣的:

- (void) vehicleConfigure 
{ 
    NSString *vehicleName = @"sports.png"; 
    UIImage *vehicleImage = [UIImage imageNamed:vehicleName]; 

    vehicleName = [vehicleName stringByReplacingOccurrencesOfString:@".png" withString:@"-mask.png"]; 
    UIImage *mask = [UIImage imageNamed:vehicleName]; 
    mask = [vehicleImage applyingMask:mask]; 

    vehicleImage = [vehicleImage coloredImageWithColor:RGB(30, 128, 255)]; 
    vehicleImage = [vehicleImage drawMask:mask]; 

    m_vehicleImageView.contentMode = UIViewContentModeScaleAspectFit; 
    m_vehicleImageView.image = vehicleImage; 
} 

我在哪裏使用貓的UIImage與方法egory:

- (UIImage *) drawMask:(UIImage *)mask 
{ 
    UIGraphicsBeginImageContextWithOptions(self.size, FALSE, 0.0); 
    [self drawInRect:(CGRect){ CGPointZero, self.size }]; 
    [mask drawInRect:(CGRect){ CGPointZero, mask.size }]; 
    UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext(); 
    UIGraphicsEndImageContext(); 
    return newImage; 
} 

- (UIImage *) applyingMask:(UIImage *)maskImage 
{ 
    CGImageRef maskRef = maskImage.CGImage; 

    CGImageRef mask = CGImageMaskCreate(CGImageGetWidth(maskRef), 
             CGImageGetHeight(maskRef), 
             CGImageGetBitsPerComponent(maskRef), 
             CGImageGetBitsPerPixel(maskRef), 
             CGImageGetBytesPerRow(maskRef), 
             CGImageGetDataProvider(maskRef), NULL, false); 

    CGImageRef masked = CGImageCreateWithMask([self CGImage], mask); 

    UIImage *im = [UIImage imageWithCGImage:masked 
             scale:[[UIScreen mainScreen] scale] 
           orientation:UIImageOrientationUp]; 

// CGImageRelease(masked); 
// CGImageRelease(maskRef); 
// CGImageRelease(mask); 

    return im; 
} 

- (UIImage *) coloredImageWithColor:(UIColor *)color 
{ 
    UIGraphicsBeginImageContext(self.size); 

    CGContextRef context = UIGraphicsGetCurrentContext(); 

    CGContextTranslateCTM(context, 0, self.size.height); 
    CGContextScaleCTM(context, 1.0, -1.0); 

    CGContextSetBlendMode(context, kCGBlendModeColorBurn); 
    CGRect rect = (CGRect){ CGPointZero, self.size }; 

    CGContextDrawImage(context, rect, self.CGImage); 
    CGContextClipToMask(context, rect, self.CGImage); 

    [color set]; 

    CGContextFillRect(context, rect); 

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

    return coloredImage; 
} 

而且結果如下:

enter image description here

+0

嗨,我們怎麼讓用戶選擇,我們要改變顏色的區域?你的幫助將非常感激。 – nithinreddy