簡體   English   中英

如何將UIImage和UILabel組合成一個圖像並保存

[英]How to combine UIImage and UILabel into one image and save

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

或者你可以檢查這個問題設置一個CGContext透明背景

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();

試試這個!

 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;

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM