UIImage图片 与 Base64 转换

UIImage图片转成base64字符串:

    UIImage *_originImage = [UIImage imageNamed:@"full_playlist_hl.png"];

    NSData *_data = UIImageJPEGRepresentation(_image, 1.0f);

    NSString *_encodedImageStr = [_data base64Encoding];

    NSLog(@"===Encoded image:\n%@", _encodedImageStr);

Base64字符串转UIImage图片:

    NSData *_decodedImageData   = [[NSData alloc] initWithBase64Encoding:_encodedImageStr];

    UIImage *_decodedImage      = [UIImage imageWithData:_decodedImageData];

    NSLog(@"===Decoded image size: %@", NSStringFromCGSize(_decodedImage.size));

转载地址:http://www.myexception.cn/mobile/1345414.html

转载自 http://blog.csdn.net/daiyelang/article/details/8638211

NSData+Base64.h

#import <Foundation/Foundation.h>

@interface NSData (Base64)

+ (NSData *)dataWithBase64EncodedString:(NSString *)string;

- (NSString *)base64EncodedStringWithWrapWidth:(NSUInteger)wrapWidth;

- (NSString *)base64EncodedString;

@end

NSData+Base64.m

#import "NSData+Base64.h"

@implementation NSData (Base64)

+ (NSData *)dataWithBase64EncodedString:(NSString *)string

{

   const char lookup[] =

    {

       99, 99, 99, 99, 99,99, 99, 99, 99, 99,99, 99, 99, 99, 99,99,

       99, 99, 99, 99, 99,99, 99, 99, 99, 99,99, 99, 99, 99, 99,99,

       99, 99, 99, 99, 99,99, 99, 99, 99, 99,99, 62, 99, 99, 99,63,

       52, 53, 54, 55, 56,57, 58, 59, 60, 61,99, 99, 99, 99, 99,99,

       99,  0,  1,  2,  3, 4,  5,  6,  7,  8, 9, 10, 11, 12, 13,14,

       15, 16, 17, 18, 19,20, 21, 22, 23, 24,25, 99, 99, 99, 99,99,

       99, 26, 27, 28, 29,30, 31, 32, 33, 34,35, 36, 37, 38, 39,40,

       41, 42, 43, 44, 45,46, 47, 48, 49, 50,51, 99, 99, 99, 99,99

    };

    NSData *inputData = [stringdataUsingEncoding:NSASCIIStringEncodingallowLossyConversion:YES];

   long long inputLength = [inputDatalength];

   const unsignedchar *inputBytes = [inputData bytes];

   long long maxOutputLength = (inputLength /4 + 1) * 3;

   NSMutableData *outputData = [NSMutableDatadataWithLength:maxOutputLength];

   unsigned char *outputBytes = (unsignedchar *)[outputData mutableBytes];

   int accumulator = 0;

   long long outputLength =0;

   unsigned char accumulated[] = {0,0, 0, 0};

   for (long long i = 0; i < inputLength; i++)

    {

       unsigned char decoded = lookup[inputBytes[i] &0x7F];

       if (decoded != 99)

        {

            accumulated[accumulator] = decoded;

           if (accumulator == 3)

            {

                outputBytes[outputLength++] = (accumulated[0] <<2) | (accumulated[1] >>4);

                outputBytes[outputLength++] = (accumulated[1] <<4) | (accumulated[2] >>2);

                outputBytes[outputLength++] = (accumulated[2] <<6) | accumulated[3];

            }

            accumulator = (accumulator +1) % 4;

        }

    }

    //handle left-over data

   if (accumulator > 0) outputBytes[outputLength] = (accumulated[0] <<2) | (accumulated[1] >>4);

   if (accumulator > 1) outputBytes[++outputLength] = (accumulated[1] <<4) | (accumulated[2] >>2);

   if (accumulator > 2) outputLength++;

    //truncate data to match actual output length

    outputData.length = outputLength;

   return outputLength? outputData: nil;

}

- (NSString *)base64EncodedStringWithWrapWidth:(NSUInteger)wrapWidth

{

    //ensure wrapWidth is a multiple of 4

    wrapWidth = (wrapWidth /4) * 4;

    constchar lookup[] ="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";

   long long inputLength = [selflength];

   const unsignedchar *inputBytes = [selfbytes];

   long long maxOutputLength = (inputLength /3 + 1) * 4;

    maxOutputLength += wrapWidth? (maxOutputLength / wrapWidth) *2: 0;

   unsigned char *outputBytes = (unsignedchar *)malloc(maxOutputLength);

   long long i;

   long long outputLength =0;

   for (i = 0; i < inputLength -2; i += 3)

    {

        outputBytes[outputLength++] = lookup[(inputBytes[i] &0xFC) >> 2];

        outputBytes[outputLength++] = lookup[((inputBytes[i] &0x03) << 4) | ((inputBytes[i +1] & 0xF0) >>4)];

        outputBytes[outputLength++] = lookup[((inputBytes[i +1] & 0x0F) <<2) | ((inputBytes[i + 2] & 0xC0) >> 6)];

        outputBytes[outputLength++] = lookup[inputBytes[i +2] & 0x3F];

        //add line break

       if (wrapWidth && (outputLength + 2) % (wrapWidth + 2) == 0)

        {

            outputBytes[outputLength++] =‘\r‘;

            outputBytes[outputLength++] =‘\n‘;

        }

    }

    //handle left-over data

   if (i == inputLength - 2)

    {

        // = terminator

        outputBytes[outputLength++] = lookup[(inputBytes[i] &0xFC) >> 2];

        outputBytes[outputLength++] = lookup[((inputBytes[i] &0x03) << 4) | ((inputBytes[i +1] & 0xF0) >>4)];

        outputBytes[outputLength++] = lookup[(inputBytes[i +1] & 0x0F) <<2];

        outputBytes[outputLength++] =  ‘=‘;

    }

   else if (i == inputLength -1)

    {

        // == terminator

        outputBytes[outputLength++] = lookup[(inputBytes[i] &0xFC) >> 2];

        outputBytes[outputLength++] = lookup[(inputBytes[i] &0x03) << 4];

        outputBytes[outputLength++] =‘=‘;

        outputBytes[outputLength++] =‘=‘;

    }

    //truncate data to match actual output length

    outputBytes =realloc(outputBytes, outputLength);

    NSString *result = [[NSStringalloc] initWithBytesNoCopy:outputByteslength:outputLength encoding:NSASCIIStringEncodingfreeWhenDone:YES];

#if !__has_feature(objc_arc)

    [resultautorelease];

#endif

   return (outputLength >= 4)? result: nil;

}

- (NSString *)base64EncodedString

{

    return [selfbase64EncodedStringWithWrapWidth:0];

}

@end

NSString+Base64.h

#import <Foundation/Foundation.h>

@interface NSString (Base64)

+ (NSString *)stringWithBase64EncodedString:(NSString *)string;

- (NSString *)base64EncodedStringWithWrapWidth:(NSUInteger)wrapWidth;

- (NSString *)base64EncodedString;

- (NSString *)base64DecodedString;

- (NSData *)base64DecodedData;

@end

NSstring+Base64.m

#import "NSString+Base64.h"

#import "NSData+Base64.h"

@implementation NSString (Base64)

+ (NSString *)stringWithBase64EncodedString:(NSString *)string

{

    NSData *data = [NSDatadataWithBase64EncodedString:string];

   if (data)

    {

        NSString *result = [[selfalloc] initWithData:dataencoding:NSUTF8StringEncoding];

#if !__has_feature(objc_arc)

        [resultautorelease];

#endif

       return result;

    }

    return nil;

}

- (NSString *)base64EncodedStringWithWrapWidth:(NSUInteger)wrapWidth

{

    NSData *data = [selfdataUsingEncoding:NSUTF8StringEncodingallowLossyConversion:YES];

    return [database64EncodedStringWithWrapWidth:wrapWidth];

}

- (NSString *)base64EncodedString

{

    NSData *data = [selfdataUsingEncoding:NSUTF8StringEncodingallowLossyConversion:YES];

    return [database64EncodedString];

}

- (NSString *)base64DecodedString

{

    return [NSStringstringWithBase64EncodedString:self];

}

- (NSData *)base64DecodedData

{

    return [NSDatadataWithBase64EncodedString:self];

}

@end
时间: 2024-11-03 20:45:49

UIImage图片 与 Base64 转换的相关文章

图片压缩base64转换 文字base64加密 上传图片会用到

// 3.图片base64转换     // 先获取压缩图片     UIImage *compressionImage = [selforiginImage:_profileImageView.imagescaleToSize:CGSizeMake(120, 120)];     // 再转换图片     NSData *data = UIImageJPEGRepresentation(compressionImage, 1.0f);     NSString *encodedImageStr

base64与图片之间的转换

实现:base64与图片之间的转换 + 上传的简单实现 1 package com.thinkive.bank.mass.plat.bus.common.util; 2 3 import java.io.File; 4 import java.io.FileInputStream; 5 import java.io.FileNotFoundException; 6 import java.io.FileOutputStream; 7 import java.io.IOException; 8 i

图片和base64编码字符串 互相转换,图片和byte数组互相转换

图片和base64编码字符串 互相转换 import sun.misc.BASE64Decoder; import sun.misc.BASE64Encoder; import java.io.*; /** * @author lishupeng * @create 2017-05-06 下午 2:56 **/ public class Base64Test { public static void main(String[] args) { String strImg = GetImageSt

java实现图片与base64字符串之间的转换

java实现图片与base64字符串之间的转换 package com.zxc.testjava.ant; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import sun.misc.BASE64Decoder; import sun.misc

[java] 图片与base64之间的互相转换

这篇文章实现的功能是,本地或者线上的图片转换成base64和base64转换成图片. 好了不多说了,直接上代码! import java.io.ByteArrayOutputStream;import java.io.FileInputStream;import java.io.FileOutputStream;import java.io.IOException;import java.io.InputStream;import java.io.OutputStream;import java

字符串与图片的Base64编码转换操作

//图片 转为 base64编码的文本 private void button1_Click(object sender, EventArgs e) { OpenFileDialog dlg = new OpenFileDialog(); dlg.Title = "选择要转换的图片"; dlg.Filter = "Image files (*.jpg;*.bmp;*.gif)|*.jpg*.jpeg;*.gif;*.bmp|AllFiles (*.*)|*.*";

VUE Base64编码图片展示与转换图片

图片的 base64 编码就是可以将一副图片数据编码成一串字符串,使用该字符串代替图像地址,使用 base64 传输图片文件可以节省一个 http 请求,图片的 base64 编码可以算是前端优化的一环 VUE Base64编码图片展示 <img :src="icon"> export default { data() { icon: 'data:image/png;base64,,XXXXX...', } } 图片在线转换Base64:http://imgbase64.d

图片与Base64的转换

图片转为Base64 // 图片转化成base64字符串 public static String GetImageStr() {// 将图片文件转化为字节数组字符串,并对其进行Base64编码处理 String imgFile = "C:/image1/2.png";// 待处理的图片 InputStream in = null; byte[] data = null; // 读取图片字节数组 try { in = new FileInputStream(imgFile); data

JAVA 将图片转换为Base64编码

这里使用的jar包是commons-codec-1.10.jar; 示例代码 1 import java.io.FileInputStream; 2 import java.io.FileOutputStream; 3 import java.io.IOException; 4 import java.io.InputStream; 5 import java.io.OutputStream; 6 7 import org.apache.commons.codec.binary.Base64;