【问题标题】:UIImage to base64 String EncodingUIImage 到 base64 字符串编码
【发布时间】:2011-04-22 19:32:26
【问题描述】:

如何将UIimage 转换为base64 编码的字符串?我找不到任何详细说明的示例或代码。

【问题讨论】:

标签: iphone objective-c


【解决方案1】:

我想知道您为什么没有找到您的问题,因为这是一个非常古老的问题并且可以找到 here

无论如何, 您需要先将 NSData 类别添加到您的项目中,这些类别可从此处获得 -

headerimplementation 然后将您的UIImage 对象转换为NSData,方法如下:

NSData *imageData = UIImageJPEGRepresentation(image, 1.0);

然后应用Base64编码将其转换为base64编码字符串:

NSString *encodedString = [imageData base64Encoding];

【讨论】:

【解决方案2】:

NSData (Base64) 自本帖上次回复以来略有变化。

你现在应该使用:

NSData *base64EncodedImage = [UIImageJPEGRepresentation(img, 0.8) base64EncodingWithLineLength:0];

【讨论】:

  • base64EncodingWithLineLength 返回 NSData
【解决方案3】:
@implementation UIImage (Extended)

- (NSString *)base64String {
    NSData * data = [UIImagePNGRepresentation(self) base64EncodedDataWithOptions:NSDataBase64Encoding64CharacterLineLength];
    return [NSString stringWithUTF8String:[data bytes]];
}

@end

【讨论】:

  • 为什么不直接使用base64EncodedStringWithOptions?
  • 所以代码看起来不像 spagety :) 一个好的习惯是避免 3 条语句相互出现,例如 [Me doSmthh:[[Something execute:@"..."] someTypeOfValue]] ;
【解决方案4】:

iOS 7 中的一些更改允许在不使用任何外部类别来支持 Base64 编码/解码的情况下完成此操作。

你可以直接使用:

- (NSString *)base64String {
    return [UIImagePNGRepresentation(self) base64EncodedStringWithOptions:NSDataBase64Encoding64CharacterLineLength];
}

【讨论】:

  • 请注意,这些方法已在 iOS 7 中引入,但 base64Encoding 已在 iOS 4 中可用。因此,除非您仍然支持 iOS 2 或 3,否则您可以使用 base64Encoding 或 @ Abizern 提到没有任何外部框架。
  • 感谢您提及后端端口。
  • 我在传递0 而不是NSDataBase64Encoding64CharacterLineLength 的选项时运气更好。这样它就不会插入任何换行符,并且可以在 UIWebView 中的 data: URI 中使用。
  • @TalkLittle 尝试使用kNilOptions。同样的东西,但更具可读性
  • NSDataBase64EncodingEndLineWithLineFeed
【解决方案5】:

您可以按照以下代码进行操作

-(NSString *)imageToNSString:(UIImage *)image
{
NSData *imageData = UIImagePNGRepresentation(image);
    return [imageData base64EncodedStringWithOptions:NSDataBase64Encoding64CharacterLineLength];
}

-(UIImage *)stringToUIImage:(NSString *)string
{
 NSData *data = [[NSData alloc]initWithBase64EncodedString:string
                                                     options:NSDataBase64DecodingIgnoreUnknownCharacters];
    return [UIImage imageWithData:data];
}

【讨论】:

    【解决方案6】:

    在 ios 中将图像转换为 base64 时,base64 编码字符串中的新行“\n”:

    使用此代码:

    UIImage* orginalImage = [info objectForKey:UIImagePickerControllerOriginalImage]; NSIndexPath *indexPath = [NSIndexPath indexPathForRow:isRowIndex inSection:isSectionIndex] ; UITableViewCell *cell = [jobstable cellForRowAtIndexPath:indexPath]; UIImageView *tableIMAGE=(UIImageView *)[cell.contentView viewWithTag:19]; tableIMAGE.image=原始图像; imageStris = [UIImageJPEGRepresentation(tableIMAGE.image,1)base64Encoding]; answersARRAY[indexPath.row] = [NSString stringWithFormat:@"-1,%@,%@",answersARRAY[indexPath.row],imageStris]; [自我dismissViewControllerAnimated:是完成:无];

    【讨论】:

      【解决方案7】:

      斯威夫特 3

      我使用 base64EncodedString()Data() 对象转换为 base64 字符串

      图片转base64字符串

          var sample = UIImage(named: "image_logo")
          let imageData:Data =  UIImagePNGRepresentation(sample!)!
          let base64String = imageData.base64EncodedString()
      

      【讨论】:

        猜你喜欢
        • 2012-05-04
        • 1970-01-01
        • 2012-03-05
        • 1970-01-01
        • 1970-01-01
        • 2018-08-15
        • 2019-06-15
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多