Programmatically create image from UILabel

前端 未结 3 1561
谎友^
谎友^ 2021-01-02 20:48

I want to use a UILabel to create an RGBA stream of bytes image representation programmatically (at runtime).

For example, I want to create a UILabel in a specific

相关标签:
3条回答
  • 2021-01-02 21:31

    You can "capture" (take a snapShot of) your UILabel, it's a subclass of UIView, so you can get its image.

    use this class "CaptureView":

    CaptureView.h

    #import <UIKit/UIKit.h>
    #import <QuartzCore/QuartzCore.h>
    #import <QuartzCore/CALayer.h>
    
    
    @interface CaptureView : UIView {
    @private
        UIImage *_imageCapture;
    }
    
    @property(nonatomic, retain) UIImage *imageCapture;
    
    // Init
    - (id)initWithView:(UIView *)view;
    
    @end
    

    CaptureView.m

    #import "CaptureView.h"
    
    // Private
    @interface CaptureView (/* Private */)
    - (void)settingImageFromView:(UIView *)view;
    @end
    
    // Public
    @implementation CaptureView
    
    @synthesize imageCapture = _imageCapture;
    
    // Standard // UILabel
    - (id)initWithFrame:(CGRect)frame {
    
        if ((self = [super initWithFrame:frame])) {
            // Initialization code.
        }
        return self;
    }
    
    // Init
    - (id)initWithView:(UIView *)view {
        if ((self = [super initWithFrame:[view frame]])) {
    //  if ((self = [super initWithFrame: CGRectMake(0, 0, 100, 100)])) {
            // Initialization code.
            [self settingImageFromView:view];
        }
        return self;  
    }
    
    /*!
     @method     imageFromView:view
     @abstract   Esegue una Grab di una intera view
     @discussion Imposta l'immagine catturata eseguendo una copia dell'intera view 
     */
    - (void)settingImageFromView:(UIView *)view {
        CGRect rect = [view bounds];  
     //   CGRect rect = CGRectMake(20, 30, 100, 100);  
        UIGraphicsBeginImageContext(rect.size);  
        CGContextRef context = UIGraphicsGetCurrentContext();  
        [view.layer renderInContext:context];  
        UIImage *imageCaptureRect;
    
        imageCaptureRect = UIGraphicsGetImageFromCurrentImageContext();  
        _imageCapture = imageCaptureRect;
    //    _imageCapture = UIGraphicsGetImageFromCurrentImageContext();  
    //    [_imageCapture retain];
        UIGraphicsEndImageContext();   
    }
    
    
    // Only override drawRect: if you perform custom drawing.
    // An empty implementation adversely affects performance during animation.
    - (void)drawRect:(CGRect)rect {
        // Drawing code.
        CGPoint accPoint = CGPointMake(0,0);
        [_imageCapture drawAtPoint:accPoint];
    }
    
    
    - (void)dealloc {
        [_imageCapture release];
        [super dealloc];
    }
    
    @end
    

    so just initiate it passing your UILabel to it, so you'll get a Class with the UIImage *imageCapture property, so just get it and use it...

    // in your class which wanna manage the captured image:
    // in .h : #import "CaptureView.h"
    CaptureView *cloneView = [[CaptureView alloc] initWithView:aUILabel];
    //  [yourUIView addSubview:cloneView]; // eventually, to see it...
    UIImage *myCapt = cloneView.imageCapture;
    // ... your code to use myCapt
    [cloneView release];
    

    EDIT:

    download this project:

    http://meronix.altervista.org/captureUILabel.zip

    the centered black-backGround text is a capture (an instance of the class: CaptureView) of the Gray-BackGround text (a UILabel) on top

    0 讨论(0)
  • 2021-01-02 21:42
    CGFloat scale  = [[[view window] screen] scale];
    CGRect  bounds = [view bounds];
    
    CGColorSpaceRef space = CGColorSpaceCreateDeviceRGB();
    CGContextRef ctx = CGBitmapContextCreate(NULL, CGRectGetWidth(bounds) * scale, CGRectGetHeight(bounds) * scale, 8, CGRectGetWidth(bounds) *
    scale * 4, space, kCGImageAlphaPremultipliedLast);
    CGContextTranslateCTM(ctx, 0.0, CGRectGetHeight(bounds) * scale);
    CGContextScaleCTM(ctx, scale, -scale);
    [[view layer] renderInContext:ctx];
    

    Then, use the bytes via CGBitmapContextGetData(myContext). No reason to bother with a UIImage

    0 讨论(0)
  • 2021-01-02 21:52

    This should work:

    UIGraphicsBeginImageContext(yourLabel.bounds.size);
    [yourLabel.layer renderInContext:UIGraphicsGetCurrentContext()];
    CGImageRef viewImage = [UIGraphicsGetImageFromCurrentImageContext() CGImage];
    UIGraphicsEndImageContext();
    

    UIGraphicsGetImageFromCurrentImageContext() returns an instance of UIImage.

    0 讨论(0)
提交回复
热议问题