10

我有 2 个 UILabel 和 2 个图像,我需要合并到一个 UIImage 中来保存。

我知道我可以用屏幕截图来做到这一点,但我的主图像是圆形的,所以如果我矫正它,它仍然会显示出锐利的边缘。

我可以这样做来组合图像:

//CGSize newImageSize = CGSizeMake(cropImage.frame.size.width, cropImage.frame.size.height);
CGSize newImageSize = CGSizeMake(480, 320);
NSLog(@"CGSize %@",NSStringFromCGSize(newImageSize));

UIGraphicsBeginImageContextWithOptions(newImageSize, NO, 0.0); //retina res
[self.viewForImg.layer renderInContext:UIGraphicsGetCurrentContext()];

UIImage *image = UIGraphicsGetImageFromCurrentImageContext();

NSData *imgData =  UIImageJPEGRepresentation(image, 0.9); //UIImagePNGRepresentation ( image ); // get JPEG representation
UIImage * imagePNG = [UIImage imageWithData:imgData]; // wrap UIImage around PNG representation

UIGraphicsEndImageContext();
return imagePNG;

但不确定如何在 UILabel 中添加。

任何回复都非常感谢。

4

3 回答 3

18

用于[myLabel.layer renderInContext:UIGraphicsGetCurrentContext()];在当前上下文中绘制。

例如:-

    UIGraphicsBeginImageContextWithOptions(newImageSize, NO, 0.0); //retina res
    [self.viewForImg.layer renderInContext:UIGraphicsGetCurrentContext()];
    [myLabel.layer renderInContext:UIGraphicsGetCurrentContext()];
    UIImage *image = UIGraphicsGetImageFromCurrentImageContext();

根据您的评论,如果您想在特定框架中绘制它,请执行以下操作,

[myLabel drawTextInRect:CGRectMake(0.0f, 0.0f, 100.0f, 50.0f)];

如果你想给背景上色,试试这个,

CGRect drawRect = CGRectMake(rect.origin.x, rect.origin.y,rect.size.width, rect.size.height); 
CGContextSetRGBFillColor(context, 100.0f/255.0f, 100.0f/255.0f, 100.0f/255.0f, 1.0f); 
CGContextFillRect(context, drawRect);

或者你可以检查这个问题Setting A CGContext Transparent Background

于 2012-11-23T02:28:21.577 回答
4
UIEdgeInsets insets = UIEdgeInsetsMake(1, 1, 1, 1);
CGSize imageSizeWithBorder = CGSizeMake(view.frame.size.width + insets.left + insets.right, view.frame.size.height + insets.top + insets.bottom);
UIGraphicsBeginImageContextWithOptions(imageSizeWithBorder, UIEdgeInsetsEqualToEdgeInsets(insets, UIEdgeInsetsZero), 0);
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextClipToRect(context, (CGRect){{insets.left, insets.top}, view.frame.size});
CGContextTranslateCTM(context, -view.frame.origin.x + insets.left, -view.frame.origin.y + insets.top);
[view.layer renderInContext:context];
UIImage *viewCopy = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();

试试这个!

于 2013-02-05T15:07:16.703 回答
2
 UIGraphicsBeginImageContextWithOptions(newImageSize, NO, scale); //retina res
        [COGI.layer renderInContext:UIGraphicsGetCurrentContext()];
        [COGI.image drawInRect:CGRectMake(0, 0, 248, 290)];
        [iconI.image drawInRect:CGRectMake(4, 20, 240, 240)];
        [stampI.image drawInRect:CGRectMake(0, -5, 248, 290)];
        [headerL drawTextInRect:CGRectMake(14, 35, 220, 40)];
        [detailL drawTextInRect:CGRectMake(16, 200, 215, 65)];

        UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
        [[UIColor redColor] set]; 
        NSData *imgData =  UIImageJPEGRepresentation(image, 1.0); //UIImagePNGRepresentation ( image ); // get JPEG representation
        UIImage * imagePNG = [UIImage imageWithData:imgData]; // wrap UIImage around PNG representation

        UIGraphicsEndImageContext();
        return imagePNG;
于 2012-11-24T09:18:51.563 回答