方法
(NSString *)base64EncodedString {
NSData *data = [self dataUsingEncoding:NSUTF8StringEncoding];
return [data base64EncodedStringWithOptions:0];
}(NSString *)base64DecodedString {
NSData *data = [[NSData alloc]initWithBase64EncodedString:self options:0];
return [[NSString alloc]initWithData:data encoding:NSUTF8StringEncoding];
}
對字符進(jìn)行Base64編解碼
NSString *sourceStr = @"www.baidu.com";
NSLog(@"sourceStr---->%@",sourceStr);
NSString *base64Str = [sourceStr base64EncodedString];
NSLog(@"Base64編碼---->%@",base64Str);
NSString *decodeStr = [base64Str base64DecodedString];
NSLog(@"Base64解碼---->%@",decodeStr);
/*
sourceStr---->www.baidu.com
Base64編碼---->d3d3LmJhaWR1LmNvbQ==
Base64解碼---->www.baidu.com
*/
對圖片進(jìn)行Base64編解碼
// UIImage圖片轉(zhuǎn)成Base64字符串:
UIImage *img = [UIImage imageNamed:@"0.jpg"];
NSData *imgData = UIImageJPEGRepresentation(img, 1.0f);
NSString *encodedImgStr = [imgData base64EncodedStringWithOptions:NSDataBase64Encoding64CharacterLineLength];
NSLog(@"encodedImgStr---->%@",encodedImgStr);
//Base64字符串轉(zhuǎn)UIImage圖片:
NSData *decodedImgData = [[NSData alloc] initWithBase64EncodedString:encodedImgStr options:NSDataBase64DecodingIgnoreUnknownCharacters];
UIImage *decodedImage = [UIImage imageWithData:decodedImgData];
UIImageView *imgV = [[UIImageView alloc] initWithFrame:CGRectMake(0, 100, [UIScreen mainScreen].bounds.size.width, [UIScreen mainScreen].bounds.size.width)];
imgV.contentMode = UIViewContentModeScaleAspectFit;
[imgV setImage:decodedImage];
[self.view addSubview:imgV];
NSLog(@"decodedImage---->%@",decodedImgData);