2017-01-30 104 views
0

我已經旋轉的UILabel(用的UILabel變換):centerLabel.transform = CGAffineTransformMakeRotation((90 * M_PI)/180);渲染的UILabel與變換

我需要渲染我的UILabel到UIImage的,但我不能使它與改造。當我渲染UILabel時,我得到了UILabel而沒有變換。我怎麼做到的?

這是我的方法:

+ (UIImage *) imageWithView:(UIView *)view andOpaque:(BOOL)opaque { 
UIGraphicsBeginImageContextWithOptions(view.bounds.size, opaque, 0.0); 
[view.layer renderInContext:UIGraphicsGetCurrentContext()]; 
UIImage * img = UIGraphicsGetImageFromCurrentImageContext(); 
UIGraphicsEndImageContext(); 
return img; 
} 

回答

0

的UILabel使圖像,試試這個代碼:

#import <QuartzCore/QuartzCore.h> 
#import <QuartzCore/CALayer.h> 

旋轉你的的UILabel

centerLabel.transform = CGAffineTransformMakeRotation((90 * M_PI)/180); 

後存儲捕獲在的UIImage

UIImage *image = [self imageWithView:centerLabel]; 

所捕獲的圖像的圖像d將不轉動。如果您想要輪流使用該圖像,則需要在捕獲UILabel後使用CGAffineTransformMakeRotation對於該UIImageView

- (UIImage *) imageWithView:(UIView *)view 
{ 
    // Create a "canvas" (image context) to draw in. 
    UIGraphicsBeginImageContextWithOptions(view.bounds.size, false, 0.0); // high res 

    // Make the CALayer to draw in our "canvas". 
    [[view layer] renderInContext: UIGraphicsGetCurrentContext()]; 

    // Fetch an UIImage of our "canvas". 
    UIImage *image = UIGraphicsGetImageFromCurrentImageContext(); 

    // Stop the "canvas" from accepting any input. 
    UIGraphicsEndImageContext(); 

    // Return the image. 
    return image; 
}