2012-08-07 195 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