画自定义资源图片

前提:app需要适配国际化

占位图上为:上传图片

难点:占位图针对不同国家需要显示不同语言的“上传图片”

用draw解决

代码如下:

+ (UIImage *)drawUploadCoverImageWithSize:(CGSize)size{
    CGRect rect = CGRectMake(0.0f, 0.0f, size.width, size.height);
    float textHeight = size.height/3;
    // 画图
    UIGraphicsBeginImageContext(size);
    CGContextRef context = UIGraphicsGetCurrentContext();
    CGContextSetFillColorWithColor(context, [UIColor whiteColor].CGColor);
    CGContextFillRect(context, rect);//白色背景
    NSString *string = @"上传封面";
    NSMutableAttributedString *attrStr = [[NSMutableAttributedString alloc]initWithString:string];
    [attrStr addAttribute:NSFontAttributeName value:[UIFont fontWithName:@"HelveticaNeue-Bold"size:30.0f] range:NSMakeRange(0, string.length)];
    [attrStr addAttribute:NSForegroundColorAttributeName value:[UIColor grayColor] range:NSMakeRange(0, string.length)];
    [attrStr drawInRect:CGRectMake(0, textHeight, size.width, textHeight)];
    NSMutableParagraphStyle *paragraph = [[NSMutableParagraphStyle alloc]init];
    paragraph.alignment = NSTextAlignmentCenter;
    [attrStr addAttribute:NSParagraphStyleAttributeName value:paragraph range:NSMakeRange(0, string.length)];
    CGImageRef imgRef = CGBitmapContextCreateImage(context);
    UIImage *image = [UIImage imageWithCGImage:imgRef];
    CGImageRelease(imgRef);
    CGContextRelease(context);
    return image;
}

你可能感兴趣的:(画自定义资源图片)