如何将NSString转换为UIImage以用于appengine blobstore [英] How to convert NSString to UIImage for appengine blobstore

查看:134
本文介绍了如何将NSString转换为UIImage以用于appengine blobstore的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我执行以下操作时,生成的图像为空

  NSLog(@string(%@)to base64NSData,目的); 
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。我需要在图像中包含一些元数据。当我将字符串编码为base64时,它不起作用。 AFHTTPRequestOperationManager后多部分请求不工作

解决方案

下面是从NSString生成图像的代码..




$ p $ +( 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];
//根据您的要求使用此图片

我希望它适合你。


when I do the following the resulting image is null

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);

How might I do this well?

Reason

I need to save an image from iOS to the Google blobstore. I need to include some metadata with the image. When I encode the string as base64 it does not work.

This is loosely a follow up to AFHTTPRequestOperationManager post multi-part request not working

解决方案

Here is the code for generating image from NSString..

Implement this method....

 +(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;
}

Call this method like...

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

I hope it work for you..

这篇关于如何将NSString转换为UIImage以用于appengine blobstore的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆