2012-08-07 85 views
0

我已经能够成功地重新绘制具有边框的UIImage,但没有边框+阴影。下面的代码成功地绘制了一个白色边框的图像,但我无法弄清楚如何在边框下包含阴影。非常感谢帮助!在UIImage上绘制边框和阴影

- (UIImage *)generatePhotoFrameWithImage:(UIImage *)image { 
CGSize newSize = CGSizeMake(image.size.width + 50, image.size.height + 60); 
UIGraphicsBeginImageContext(newSize); 
CGRect rect = CGRectMake(25, 35, image.size.width, image.size.height); 
[image drawInRect:rect blendMode:kCGBlendModeNormal alpha:1.0]; 

CGContextRef context = UIGraphicsGetCurrentContext(); 
CGContextSetRGBStrokeColor(context, 1.0, 1.0, 1.0, 1.0); 
CGContextStrokeRectWithWidth(context, CGRectMake(25, 35, image.size.width, image.size.height), 50); 

//CGContextSetShadowWithColor(context, CGSizeMake(0, -60), 5, [UIColor blackColor].CGColor); 

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

return result; } 

PS:我没有使用UIImageView,因为我需要保留完整的图像质量。

+0

“UIImageView”以何种方式失去图像质量? – 2012-08-07 03:59:24

+0

我需要能够在应用阴影/边框后将图像输出到文件。您可以将UIImageView呈现为PNG/BMP,但它们只能呈现UIImageView的大小。 – thedeveloper3124 2012-12-30 03:14:23

回答

9

您需要先调用CGContextSetShadowWithColor,然后再对边框进行描边,因此边框会投下阴影。

如果您不希望边框在图像上投射阴影,则需要设置透明度图层,在该图层中绘制图像和边框。您可以阅读关于Quartz 2D Programming Guide中的透明度图层。

如果要保留图像质量,您需要使用UIGraphicsBeginImageContextWithOptions并传递原始图像的比例。

- (UIImage *)generatePhotoFrameWithImage:(UIImage *)image { 
    CGSize newSize = CGSizeMake(image.size.width + 50, image.size.height + 60); 
    CGRect rect = CGRectMake(25, 35, image.size.width, image.size.height); 

    UIGraphicsBeginImageContextWithOptions(newSize, NO, image.scale); { 
     CGContextRef context = UIGraphicsGetCurrentContext(); 

     CGContextBeginTransparencyLayer(context, NULL); { 
      [image drawInRect:rect blendMode:kCGBlendModeNormal alpha:1.0]; 

      CGContextSetRGBStrokeColor(context, 1.0, 1.0, 1.0, 1.0); 
      CGContextSetShadowWithColor(context, CGSizeMake(0, 5), 5, [UIColor blackColor].CGColor); 
      CGContextStrokeRectWithWidth(context, CGRectMake(25, 35, image.size.width, image.size.height), 50); 
     } CGContextEndTransparencyLayer(context); 
    } 
    UIImage *result = UIGraphicsGetImageFromCurrentImageContext(); 
    UIGraphicsEndImageContext(); 

    return result; 
} 
+2

爱的缩进风格! +1 – 2013-11-25 21:50:00

4

谢谢Rob为我指出了正确的方向。这是最终的代码,它围绕图像绘制白色边框,然后在边框外围绘制浅灰色阴影

- (UIImage *)generatePhotoStackWithImage:(UIImage *)image { 
CGSize newSize = CGSizeMake(image.size.width + 70, image.size.height + 70); 
CGRect rect = CGRectMake(25, 25, image.size.width, image.size.height); 

UIGraphicsBeginImageContextWithOptions(newSize, NO, image.scale); { 
    CGContextRef context = UIGraphicsGetCurrentContext(); 
    //Shadow 
    CGContextSetShadowWithColor(context, CGSizeMake(0, 0), 10, [UIColor colorWithRed:0.0f green:0.0f blue:0.0f alpha:0.5f].CGColor); 

    CGContextBeginTransparencyLayer (context, NULL); 
    //Draw 
    [image drawInRect:rect blendMode:kCGBlendModeNormal alpha:1.0]; 
    CGContextSetRGBStrokeColor(context, 1.0, 1.0, 1.0, 1.0); 
    CGContextStrokeRectWithWidth(context, rect, 40); 
    CGContextEndTransparencyLayer(context); 
} 
UIImage *result = UIGraphicsGetImageFromCurrentImageContext(); 
UIGraphicsEndImageContext(); 

return result; 
} 
+1

你应该接受他的回答。 ;) – Baub 2012-08-07 19:48:05