如何将NSString转换为UIImage的appengine blobstore

当我做下面的结果图像为空

NSLog(@"string (%@) to base64NSData",object); NSData *base64Data = [[object dataUsingEncoding:NSUTF8StringEncoding] base64EncodedDataWithOptions:0]; NSLog(@"NSData (%@) to UIImage",base64Data); UIImage *image = [UIImage imageWithData:base64Data]; NSLog(@"my image is: %@",image); 

我该怎么做呢?

原因

我需要将图像从iOS保存到Google blobstore。 我需要在图像中包含一些元数据。 当我编码string为base64它不起作用。

这大致是AFHTTPRequestOperationManager后续多部分请求不起作用

这里是从NSString生成图像的代码..

实现这个方法….

  +(UIImage *)imageFromText:(NSString *)text { UIFont *font = [UIFont systemFontOfSize:20.0]; CGSize size = [text sizeWithAttributes: @{NSFontAttributeName: [UIFont systemFontOfSize:20.0f]}]; if (UIGraphicsBeginImageContextWithOptions != NULL) UIGraphicsBeginImageContextWithOptions(size,NO,0.0); else UIGraphicsBeginImageContext(size); [text drawAtPoint:CGPointMake(0.0, 0.0) withFont:font]; // [text drawAtPoint:CGPointMake(0.0, 0.0) forWidth:CGPointMake(0, 0) withFont:font fontSize:nil lineBreakMode:NSLineBreakByWordWrapping baselineAdjustment:NSTextAlignmentCenter]; UIImage *image = UIGraphicsGetImageFromCurrentImageContext(); UIGraphicsEndImageContext(); return image; } 

像这样调用这个方法…

  UIImage *image; image = [self imageFromText:@"Good Morning]; //Use this image as per your requirement 

我希望它为你工作..

这里是我用来解决我的问题的UIImage类别

http://hasseg.org/blog/post/692/use-unicode-emoji-as-icons-in-native-ios-apps/

您可以修改方法来传递额外的参数,如font,color,backgroundColor,如下所示:

 + (UIImage *) hg_imageFromString:(NSString *)string withFont:(UIFont*)font textColor:(UIColor*)color andBgColor:(UIColor*)bgColor; { UILabel *label = [[UILabel alloc] init]; label.text = string; label.opaque = NO; if(bgColor != nil) label.backgroundColor = bgColor; if(color != nil) label.textColor = color; if(font != nil) label.font = font; else label.font = [UIFont systemFontOfSize:17.0]; CGSize size = [string sizeWithAttributes:@{NSFontAttributeName: label.font}]; CGSize measuredSize = CGSizeMake(ceilf(size.width), ceilf(size.height)); label.frame = CGRectMake(0, 0, measuredSize.width, measuredSize.height); return [UIImage hg_imageFromView:label]; }