簡體   English   中英

如何向UIImage或UIImageView或UIView添加外部光暈

[英]How do I add an outer glow to a UIImage or UIImageView or UIView

我想為UIImage / UIImageView / UIView添加一個FADED陰影/外部光暈但我根本不知道Core Graphics

編輯:請幫助!!

采用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

您可以使用這個,簡單快速,使用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;

作為性能考慮因素,iPhone OS不支持shadowColor,shadowOffset,shadowOpacity和shadowRadius屬性,這些屬性通常可以在Cocoa中使用。 大多數人復制他們想要發光幾次的形狀,每次降低不透明度並一次偏移一個像素的形狀以模擬發光的外觀。 如果你的發光不需要很大,你就無法區分它們。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM