2012-03-03 87 views
3

我可能會指出Objective-C中的繪圖和渲染是我的弱點。現在,這是我的問題。如何變暗PNG?

我想爲我的遊戲添加「白天/夜晚」功能。它在地圖上有很多對象。每個對象都是包含變量和一些UIImageView中的一些數據的UIView:精靈,並且一些對象具有隱藏的環(用於顯示選擇)。

我希望能夠使UIView的內容變暗,但我無法弄清楚如何。精靈是一個具有透明度的PNG。我剛剛設法在sprite後面添加一個黑色矩形:

CGContextRef ctx = UIGraphicsGetCurrentContext(); 
CGContextSaveGState(ctx); 

CGContextSetRGBFillColor(ctx, 0, 0, 0, 0.5); 
CGContextFillRect(ctx, rect); 
CGContextRestoreGState(ctx); 

正如我讀過的,這應該在drawRect方法中完成。請幫助!

如果您想更好地理解我的場景,我正在嘗試這樣做的應用程序在App Store中稱爲「Kipos」。

回答

2

Floris497的方法是一次對多個圖像進行全面變暗的好策略(在這種情況下可能更多)。但是,這裏有一個通用的方法來生成較暗的UIImage(同時尊重Alpha像素):

+ (UIImage *)darkenImage:(UIImage *)image toLevel:(CGFloat)level 
{ 
    // Create a temporary view to act as a darkening layer 
    CGRect frame = CGRectMake(0.0, 0.0, image.size.width, image.size.height); 
    UIView *tempView = [[UIView alloc] initWithFrame:frame]; 
    tempView.backgroundColor = [UIColor blackColor]; 
    tempView.alpha = level; 

    // Draw the image into a new graphics context 
    UIGraphicsBeginImageContext(frame.size); 
    CGContextRef context = UIGraphicsGetCurrentContext(); 
    [image drawInRect:frame]; 

    // Flip the context vertically so we can draw the dark layer via a mask that 
    // aligns with the image's alpha pixels (Quartz uses flipped coordinates) 
    CGContextTranslateCTM(context, 0, frame.size.height); 
    CGContextScaleCTM(context, 1.0, -1.0); 
    CGContextClipToMask(context, frame, image.CGImage); 
    [tempView.layer renderInContext:context]; 

    // Produce a new image from this context 
    CGImageRef imageRef = CGBitmapContextCreateImage(context); 
    UIImage *toReturn = [UIImage imageWithCGImage:imageRef]; 
    CGImageRelease(imageRef); 
    UIGraphicsEndImageContext(); 
    [tempView release]; 
    return toReturn; 
} 
+0

哇,非常感謝!評論確實有幫助。然後,我將能夠調整每個對象的「亮度」級別,這是我的主要目標。榮譽給你,先生! – 2012-08-26 17:05:52

0

在它上面繪製一個UIView(黑的),並設置「啓用用戶交互」,以無

希望你可以做這個事情。

然後用這個,使其黑暗

[UIView animateWithDuration:2 
         animations:^{nightView.alpha = 0.4;} 
          completion:^(BOOL finished){ NSLog(@"done making it dark"); ]; }]; 

,使其光

[UIView animateWithDuration:2 
         animations:^{nightView.alpha = 0.0;} 
          completion:^(BOOL finished){ NSLog(@"done making it light again"); ]; }]; 
+0

但是圖像不是矩形,它是不規則的,就像一個字符。這將覆蓋整個黑色的矩形,不是嗎? – 2012-03-03 22:16:38

+0

把它放在你的遊戲視圖之上,它在夜間半透明,一天完全透明,所以你看不到它。如果那是你的意思? – Floris497 2012-03-03 22:19:42

+0

嗯。尼斯。我會嘗試的。將用戶交互設置爲NO會讓觸摸通過? – 2012-03-03 22:24:35

1

的最佳方式將是一個核心圖像過濾器添加到黑暗的圖層。你可以使用CIExposureAdjust

CIFilter *filter = [CIFilter filterWithName:@"CIExposureAdjust"]; 
[filter setDefaults]; 
[filter setValue:[NSNumber numberWithFloat:-2.0] forKey:@"inputEV"]; 
view.layer.filters = [NSArray arrayWithObject:filter]; 
+0

我幾乎不懂繪圖,圖層等任何東西 這將進入drawRect方法? – 2012-03-03 22:37:00

+0

應該如何添加圖層?然後,應該如何將圖像和內容添加到它? – 2012-03-03 22:37:25

+0

無論你在哪裏設置視圖(通常在UIViewController中),都可以使用它。不過,它確實會減慢你在視圖中做的所有動畫。 – joerick 2012-03-03 22:40:14

1

這裏是如何做到這一點:

// inputEV controlls the exposure, the lower the darker (e.g "-1" -> dark) 
-(UIImage*)adjustImage:(UIImage*)image exposure:(float)inputEV 
{ 
    CIImage *inputImage = [[CIImage alloc] initWithCGImage:[image CGImage]]; 
    UIImageOrientation originalOrientation = image.imageOrientation; 

    CIFilter* adjustmentFilter = [CIFilter filterWithName:@"CIExposureAdjust"]; 
    [adjustmentFilter setDefaults]; 
    [adjustmentFilter setValue:inputImage forKey:@"inputImage"]; 
    [adjustmentFilter setValue:[NSNumber numberWithFloat:-1.0] forKey:@"inputEV"]; 

    CIImage *outputImage = [adjustmentFilter valueForKey:@"outputImage"]; 
    CIContext* context = [CIContext contextWithOptions:nil]; 
    CGImageRef imgRef = [context createCGImage:outputImage fromRect:outputImage.extent] ; 

    UIImage* img = [[UIImage alloc] initWithCGImage:imgRef scale:1.0 orientation:originalOrientation]; 

    CGImageRelease(imgRef); 

    return img;  
} 

記得導入:

#import <QuartzCore/Quartzcore.h> 

並添加CoreGraphicsCoreImage框架到您的項目。 使用iOS 5.1測試iPhone 3GS CIFilter從iOS 5.0開始可用。

+0

感謝您的詳細解答!我似乎正是我所期待的! :) 我會試試看,檢查它是否有效/我能夠實現它,如果有的話,我會將其標記爲正確的答案。 – 2012-08-04 20:53:13

+0

我希望它可以幫助:) – RaffAl 2012-08-06 10:40:03