【问题标题】:How to convert NSString to UIImage for appengine blobstore如何将 NSString 转换为 UIImage 以用于 appengine blobstore
【发布时间】:2014-09-26 10:27:49
【问题描述】:

当我执行以下操作时,结果图像为空

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。我需要在图像中包含一些元数据。当我将字符串编码为 base64 时,它不起作用。

这是对AFHTTPRequestOperationManager post multi-part request not working的大致跟进

【问题讨论】:

标签: ios objective-c google-app-engine google-cloud-endpoints blobstore


【解决方案1】:

这是从 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 

我希望它对你有用..

【讨论】:

    【解决方案2】:

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

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

    您可以修改方法以传递额外的参数,如字体、颜色、背景颜色,如下所示:

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

    【讨论】:

      猜你喜欢
      • 2010-12-21
      • 1970-01-01
      • 2023-03-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-12-21
      • 2012-02-10
      • 2013-07-24
      相关资源
      最近更新 更多