Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/iphone/39.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Iphone 如何将UIImage和UILabel组合成一个图像并保存_Iphone_Ios_Xcode_Uiimageview_Uiimage - Fatal编程技术网

Iphone 如何将UIImage和UILabel组合成一个图像并保存

Iphone 如何将UIImage和UILabel组合成一个图像并保存,iphone,ios,xcode,uiimageview,uiimage,Iphone,Ios,Xcode,Uiimageview,Uiimage,我有2个UILabel和2个图像,需要合并到一个UIImage中保存 我知道我可以用屏幕截图来做,但是我的主图像是圆形的,所以如果我把它弄直,它仍然会显示出锐利的边缘 我可以这样做来组合图像: //CGSize newImageSize = CGSizeMake(cropImage.frame.size.width, cropImage.frame.size.height); CGSize newImageSize = CGSizeMake(480, 320); NSLog(@"CGSize %

我有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


非常感谢您的回复。

使用
[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);
或者你可以检查这个问题


试试这个

谢谢ACB的回复,但是这些并不能回答我的问题。我使用了[myLabel drawTextInRect:CGRectMake(14,35,220,40)]@德斯蒙德,是的,这也是一个选择。我还想加上这个,然后决定不加,因为这应该足以画出标签。如果问题是从一个特定的rect中提取,这是正确的,但在问题中看不到。不用担心,我认为我的问题不够清楚。谢谢:)顺便问一下,您知道如何从CurrentImageContext更改UIGraphicsGetImageFromCurrentImageContext的背景色吗?当我保存图像时,背景是白色的;CGContextSetRGBFillColor(上下文,100.0f/255.0f,100.0f/255.0f,100.0f/255.0f,1.0f);CGContextFillRect(上下文,drawRect)或检查此项,
 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;
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();