【问题标题】:Hot can I add a shadow to an image using CoreGraphics on the iPhone?Hot 我可以在 iPhone 上使用 CoreGraphics 为图像添加阴影吗?
【发布时间】:2011-05-22 13:25:47
【问题描述】:

Core Graphics 在蒙版图像下绘制阴影时遇到问题。

我编写了一个方法,使用蒙版将灰色图像(实际上它是一个图标)变成白色。现在我想在它下面画一个阴影。

到目前为止,这是我的代码:

CGContextRef context = CGBitmapContextCreate(NULL, imageRect.size.width, imageRect.size.height, 8, 0, CGImageGetColorSpace(image.CGImage), kCGImageAlphaPremultipliedLast);

CGContextSetRGBFillColor(context, 1, 1, 1, 1);
CGContextFillRect(context, imageRect);

CGContextClipToMask(context, imageRect, image.CGImage);

CGContextSetRGBFillColor(context, 0, 0, 0, 1);
CGContextFillRect(context, imageRect);

CGImageRef bwCGImage = CGBitmapContextCreateImage(context);
UIImage *bwImage = [UIImage imageWithCGImage:bwCGImage scale:image.scale orientation:image.imageOrientation];

CGContextRelease(context);
CGImageRelease(bwCGImage);


UIGraphicsBeginImageContextWithOptions(image.size, NO, 0.0);
[[UIColor whiteColor] set];
UIRectFill(CGRectMake(0, 0, image.size.width, image.size.height));

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


CGImageRef imageMask = CGImageMaskCreate(CGImageGetWidth(bwImage.CGImage), CGImageGetHeight(bwImage.CGImage), CGImageGetBitsPerComponent(bwImage.CGImage), CGImageGetBitsPerPixel(bwImage.CGImage), CGImageGetBytesPerRow(bwImage.CGImage), CGImageGetDataProvider(bwImage.CGImage), NULL, YES);

CGImageRef normalImageRef = CGImageCreateWithMask(normalBackgroundImage.CGImage, imageMask);

UIImage *normalImage = [UIImage imageWithCGImage:normalImageRef scale:image.scale orientation:image.imageOrientation];


CGImageRelease(imageMask);
CGImageRelease(normalImageRef);

实际上一切正常。只有影子不见了。怎么画?

【问题讨论】:

    标签: iphone image core-graphics shadow


    【解决方案1】:

    我始终推荐 Apple 的出色 Quartz 2D Guide。考虑到您的问题,您应该阅读Shadows Chapter

    它包括一个很棒的演示(包括 cmets)

    void MyDrawWithShadows (CGContextRef myContext, // 1
                             float wd, float ht);
    {
        CGSize          myShadowOffset = CGSizeMake (-15,  20);// 2
        float           myColorValues[] = {1, 0, 0, .6};// 3
        CGColorRef      myColor;// 4
        CGColorSpaceRef myColorSpace;// 5
    
        CGContextSaveGState(myContext);// 6
    
        CGContextSetShadow (myContext, myShadowOffset, 5); // 7
        // Your drawing code here// 8
        CGContextSetRGBFillColor (myContext, 0, 1, 0, 1);
        CGContextFillRect (myContext, CGRectMake (wd/3 + 75, ht/2 , wd/4, ht/4));
    
        myColorSpace = CGColorSpaceCreateDeviceRGB ();// 9
        myColor = CGColorCreate (myColorSpace, myColorValues);// 10
        CGContextSetShadowWithColor (myContext, myShadowOffset, 5, myColor);// 11
        // Your drawing code here// 12
        CGContextSetRGBFillColor (myContext, 0, 0, 1, 1);
        CGContextFillRect (myContext, CGRectMake (wd/3-75,ht/2-100,wd/4,ht/4));
    
        CGColorRelease (myColor);// 13
        CGColorSpaceRelease (myColorSpace); // 14
    
        CGContextRestoreGState(myContext);// 15
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-12-19
      • 2017-04-16
      • 2011-02-12
      • 1970-01-01
      • 2014-10-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多