zoukankan      html  css  js  c++  java
  • 图片 64 位编码 转换 小记


    accepted

    Swift

    First we need to have image's NSData

    //Use image name from bundle to create NSData
    let image : UIImage = UIImage(named:"imageNameHere")!
    //Now use image to create into NSData format
    let imageData:NSData = UIImagePNGRepresentation(image)!
    
    //OR next possibility
    
    //Use image's path to create NSData
    let url:NSURL = NSURL(string : "urlHere")!
    //Now use image to create into NSData format
    let imageData:NSData = NSData.init(contentsOfURL: url)!

    Swift 2.0 > Encoding

    let strBase64:String = imageData.base64EncodedStringWithOptions(.Encoding64CharacterLineLength)

    Swift 2.0 > Decoding

    let dataDecoded:NSData = NSData(base64EncodedString: strBase64, options: NSDataBase64DecodingOptions.IgnoreUnknownCharacters)!

    Encoding :

    let strBase64 = imageData.base64EncodedStringWithOptions(.allZeros)
    print(strBase64)

    Decoding :

    let dataDecoded:NSData = NSData(base64EncodedString: strBase64, options: NSDataBase64DecodingOptions(rawValue: 0))!
    let decodedimage:UIImage = UIImage(data: dataDecoded)!
    print(decodedimage)
    yourImageView.image = decodedimage

    Objective-C

    iOS7 > version

    You can use NSData's base64EncodedStringWithOptions

    Encoding :

    - (NSString *)encodeToBase64String:(UIImage *)image {
     return [UIImagePNGRepresentation(image) base64EncodedStringWithOptions:NSDataBase64Encoding64CharacterLineLength];
    }

    Decoding :

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

    iOS 6.1 and < version

    First Option : Use this link to encode and decode image

    Add Base64 class in your project.

    Encoding :

     NSData* data = UIImageJPEGRepresentation(yourImage, 1.0f);
     NSString *strEncoded = [Base64 encode:data];

    Decoding :

     NSData* data = [Base64 decode:strEncoded ];;
     image.image = [UIImage imageWithData:data];

    Another Option: Use QSUtilities for encoding and decoding

    参考链接:

    1.https://stackoverflow.com/questions/11251340/convert-between-uiimage-and-base64-string

    2.https://matrixzk.github.io/blog/20150122/load-image-encoded-with-base64/

  • 相关阅读:
    Linux磁盘分区实例演示
    浅谈Linux下的rpm
    You have 1 unapplied migration(s). Your project may not work properly until you apply the migrations for app(s): shopadmin. Run 'python manage.py migrate' to apply them.
    Xshell Linux常用命令
    OSError: mysql_config not found
    AttributeError: module 'datetime' has no attribute 'now'
    CentOS查看进程端口号以及kill操作
    nginx报错 nginx: [alert] kill(25903, 1) failed (3: No such process)
    3D 散点图的绘制
    关系数据库和非关系型数据
  • 原文地址:https://www.cnblogs.com/Jenaral/p/5772559.html
Copyright © 2011-2022 走看看