`

iOS:NSString生成UIImage

阅读更多

-(UIImage *)imageFromText:(NSString *)text width:(float)width height:(float)height
{
    // set the font type and size
    UIFont *font = [UIFont systemFontOfSize:12.0]; 
    CGSize size  = CGSizeMake(width, height);// [text sizeWithFont:font];
   
    // check if UIGraphicsBeginImageContextWithOptions is available (iOS is 4.0+)
    if (UIGraphicsBeginImageContextWithOptions != NULL)
        UIGraphicsBeginImageContextWithOptions(size,NO,0.0);
    else
        // iOS is < 4.0
        UIGraphicsBeginImageContext(size);
   
    // optional: add a shadow, to avoid clipping the shadow you should make the context size bigger

    CGContextRef ctx = UIGraphicsGetCurrentContext();
    CGContextSetShadowWithColor(ctx, CGSizeMake(1.0, 1.0), 5.0, [[UIColor grayColor] CGColor]);
   
    // draw in context, you can use  drawInRect/drawAtPoint:withFont:
    //[text drawAtPoint:CGPointMake(0.0, 0.0) withFont:font];
    [text drawInRect:CGRectMake(0, 0, width, height) withFont:font];
   
    // transfer image
    UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
    [image retain];
    UIGraphicsEndImageContext();   
   
    return image;
}

分享到:
评论
1 楼 信梦缘 2011-11-17  
学习了。。。

相关推荐

    iOSCreateCode:iOS的条码生成

    ###iOS条形码和二维码的生成 ###条形码: ###导入头文件: #import "UIImage-NKDBarcode.h" #import "NKDCode128Barcode.h" 生成条形码的Image NSString *codeStr = @"90876543212276"; UIImage *barImage = [UIImage ...

    ios开发小技巧

    iOS开发之UIlabel多行文字自动换行 (自动折行) UIView *footerView = [[UIView alloc]initWithFrame:CGRectMake(10, 100, 300, 180)]; UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(10, 100, 300, ...

    iOS中生成指定大小、指定颜色的二维码和条形码方法详解

    iOS7.0之后可以利用系统原生 API 生成二维码, iOS8.0之后可以生成条形码, 系统默认生成的颜色是黑色. 在这里, 利用以下方法可以生成指定...+ (UIImage *)qrCodeImageWithContent:(NSString *)content codeImageSize:(C

    ios-超简单一个方法实现播放动画.zip

    UIImage *image = [UIImage imageNamed:string]; // 将图片加入数组 [array addObject:image]; } // 1.3把数组存入UIImageView中 self.imageView.animati // 1.4 option self.imageView.animati // ...

    IOS 创建彩色二维码实例详解

    IOS 创建彩色二维码 因为系统创建的二维码默认都是黑色的,所以突然想改变一下二维码颜色,具体...+ (UIImage *)createQRCode:(NSString *)string andSize:(CGSize)size andColor:(UIColor *)color { UIImage *qrco

    iOS开发生成二维码图片(附中间带有小图标二维码)

    + (UIImage *)createQRCodeWithString:(NSString *)QRStering withImgSize:(CGFloat)imageFloat; 1.2 方法实现 /** 生成二维码 QRStering:字符串 imageFloat:二维码图片大小 */ + (UIImage *)...

    二维码扫描功能

    现在越来越多的App添加了一个扫码的功能,方便而且很实用,但是目前对弈iOS来说有些比较知名的库(比如ZXingObjC)库都比较大,而实际上,在iOS 7.0之后系统在AVFoundation框架中就已经实现了对于二维码扫描和生成的...

    ios开发记录

    //CGAffineTransformMakeRotation 方法的作用就是传进去一个角度(计量单位不是度,是弧度),方法内部帮你生成一个这个角度所对应的旋转矩阵 //rotate 旋转 CGAffineTransform a = ...

    地图上显示用户头像

    static NSString *customReuseIndetifier = @"customReuseIndetifier"; CusAnnotationView *annotationView = (CusAnnotationView *)[mapView dequeueReusableAnnotationViewWithIdentifier:customReuseIndetifier...

    (0140)-iOS/iPhone/iPAD/iPod源代码-其他(Others)-QR Code Encoder

    将字符串生成二维码。能够将一个字符串(NSString)转换成一个二维码图像(UIImage)。代码中用到了类库libqrencode。 注意:请在Mac下解压使用

Global site tag (gtag.js) - Google Analytics