我試圖給圖像添加一個小陰影,很像App Store中的圖標陰影。現在我正在使用下面的代碼繞過我的圖像的角落。有誰知道我可以如何調整它來增加一個小影子?UIImage Shadow Trouble
- (UIImage *)roundCornersOfImage:(UIImage *)source height:(int)height width:(int)width {
int w = width;
int h = height;
CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
CGContextRef imageContext = CGBitmapContextCreate(NULL, w, h, 8, 4 * w, colorSpace, kCGImageAlphaPremultipliedFirst);
CGContextBeginPath(imageContext);
CGRect rect = CGRectMake(0, 0, w, h);
addRoundedRectToPath(imageContext, rect, 10, 10);
CGContextClosePath(imageContext);
CGContextClip(imageContext);
CGContextDrawImage(imageContext, CGRectMake(0, 0, w, h), source.CGImage);
CGImageRef imageMasked = CGBitmapContextCreateImage(imageContext);
CGContextRelease(imageContext);
CGColorSpaceRelease(colorSpace);
return [UIImage imageWithCGImage:imageMasked];
}
「addRoundedRectToPath」指的是另一種明顯四捨五入的方法。
CGContextSetShadowWithColor的參數2是CGSize。因此它應該是CGContextSetShadowWithColor(imageContext,CGSizeMake(3,3),2,shadowColor); – samvermette 2010-05-28 20:38:47