2010-02-17 62 views

回答

8

採用Cirrostratus概述的方法,保留其緩存副本,然後應用轉換以在拖動時更改圖像的大小和/或位置。

(警告,這是不是功能/測試的代碼,但應該讓你開始)

-(UIImage*)addGlowToImage:(UIImage*)imageInput; 
{ 
    CGRect newSize = imageInput.bounds; 
    CGImageRef theImage = imageInput.CGImage; 

    // expand the size to handle the "glow" 
    newSize.size.width += 6.0; 
    newSize.size.height += 6.0; 
    UIGraphicsBeginImageContext(newSize); 
    CGContextRef ctx = UIGraphicsGetCurrentContext(); 

    CGContextBeginTransparencyLayerWithRect(ctx, newSize, NULL); 
    CGContextClearRect(ctx, newSize); 

    // you can repeat this process to build glow. 
    CGContextDrawImage(ctx, newSize, theImage); 
    CGContextSetAlpha(ctx, 0.2); 

    CGContextEndTransparencyLayer(ctx); 

    // draw the original image into the context, offset to be centered; 
    CGRect centerRect = inputImage.bounds; 
    centerRect.origin.x += 3.0; 
    centerRect.origin.y += 3.0; 
    CGContextDrawImage(ctx, centerRect, theImage); 

    result = UIGraphicsGetImageFromCurrentImageContext(); 
    UIGraphicsEndImageContext(); 

    return result; 
} 

然後在你的方法,而縮放,你會做這樣的事情:

// assumes UIImage *cachedImage = [self addGlowToImage:origImage]; has been called already. 
// assumes ivars for scale exists 

    CGRect newRect = cachedImage.bounds; 
    newRect.size.width += scale; 
    newRect.size.height += scale; 

    [cachedImage drawInRect:newRect]; // image will be scaled to fill destination rectangle. 

絕對需要看看蘋果文檔。一個好的出發地點是Quartz 2D Programming Guide

+0

好的,不完全是我想要的,但是,我會接受,我想要淡出。 – Jaba 2010-02-28 00:10:34

+1

如果更改顏色,也可以使用陰影屬性作爲輝光。簡單而簡單。 – zekel 2012-10-11 23:06:21

+1

這種代碼在很多方面都是錯誤的... – 2014-01-27 13:26:39

2

作爲性能方面的考慮因素,iPhone OS不支持shadowColor,shadowOffset,shadowOpacity和shadowRadius屬性,您可以在Cocoa中正常使用該屬性。大多數人在每次降低不透明度並一次偏移一個像素的形狀以模擬發光的外觀時,複製想要發光的形狀數次。如果你的發光不需要很大,你就不能分辨出它們的區別。

+0

這會佔用很多的性能,如果視圖每秒更新多次。我正在使用'touchesMoved'事件改變尺寸。 – Jaba 2010-02-24 17:25:25

4

您可以使用此,簡單而快速的,這個工程使用的UIView,解開,等:

UIView *shadowView = [[UIView alloc] initWithFrame:CGRectMake(100, 66, 112, 112)]; 
shadowView.layer.shadowColor = [[UIColor blackColor] CGColor]; 
shadowView.layer.shadowOpacity = .4; 
shadowView.layer.shadowOffset = CGSizeZero; 
shadowView.layer.masksToBounds = NO; 

如果,您可以用收音機,補充一點:

shadowView.layer.shadowRadius = 10.0f; 
相關問題