以编程方式从UILabel创建图像

Nek*_*ios 6 iphone opengl-es core-graphics uikit uilabel

我想使用UILabel以编程方式(在运行时)创建RGBA字节流图像表示.

例如,我想创建一个特定字体和特定文本的UILabel,将其转换为RGBA无符号字节的NSData,然后从那一点我可以轻松地将其转换为OpenGL纹理并显示它然而我想要.

重要的是我可以知道结果图像的尺寸,尽管如果绝对必要,我可以创建一个非常宽的"空白"画布来渲染它,然后在运行时通过检查字节来检测宽度和高度.

如果我可以通过UIImage/CG实现这一点,那将是一个明确的优势 - 并且具有多个渲染目标的解决方案是不合理/期望的.

编辑:放置赏金; 我最初从meronix那里得到了这个好看的答案,但现在我有时间试试它并且它无法正常工作.我需要这个"昨天"工作,我非常沮丧.这里有一些细微差别,知道Cocoa绘画状态比我更好的人肯定会抓住.

mer*_*nix 7

你可以"捕捉"(快速拍摄)你的UILabel,它是UIView的子类,所以你可以得到它的图像.

使用此类"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
Run Code Online (Sandbox Code Playgroud)

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
Run Code Online (Sandbox Code Playgroud)

所以只是启动它将你的UILabel传递给它,所以你将获得一个带有UIImage*imageCapture属性的类,所以只需获取它并使用它......

// 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];
Run Code Online (Sandbox Code Playgroud)

编辑:

下载这个项目:

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

居中的black-backGround文本是顶部的Gray-BackGround文本(UILabel)的捕获(类的实例:CaptureView)


Ale*_*olo 5

这应该工作:

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

UIGraphicsGetImageFromCurrentImageContext()返回一个实例UIImage.


Nek*_*ios 1

CGFloat 比例尺 = [[[查看窗口]屏幕]比例尺];
CGRect 边界 = [视图边界];

CGColorSpaceRef 空间 = CGColorSpaceCreateDeviceRGB();
CGContextRef ctx = CGBitmapContextCreate(NULL, CGRectGetWidth(bounds) * 缩放比例, CGRectGetHeight(bounds) * 缩放比例, 8, CGRectGetWidth(bounds) *
比例 * 4, 空间, kCGImageAlphaPremultipliedLast);
CGContextTranslateCTM(ctx, 0.0, CGRectGetHeight(bounds) * 比例);
CGContextScaleCTM(ctx, 比例, -比例);
[[视图层] renderInContext:ctx];

然后,通过 来使用字节CGBitmapContextGetData(myContext)。没有理由为 UIImage 烦恼