빠른
먼저 이미지의 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)!
스위프트 2.0> 인코딩
let strBase64:String = imageData.base64EncodedStringWithOptions(.Encoding64CharacterLineLength)
스위프트 2.0> 디코딩
let dataDecoded:NSData = NSData(base64EncodedString: strBase64, options: NSDataBase64DecodingOptions.IgnoreUnknownCharacters)!
스위프트 3.0> 디코딩
let dataDecoded : Data = Data(base64Encoded: strBase64, options: .ignoreUnknownCharacters)!
인코딩 :
let strBase64 = imageData.base64EncodedString(options: .lineLength64Characters)
print(strBase64)
디코딩 :
let dataDecoded:NSData = NSData(base64EncodedString: strBase64, options: NSDataBase64DecodingOptions(rawValue: 0))!
let decodedimage:UIImage = UIImage(data: dataDecoded)!
print(decodedimage)
yourImageView.image = decodedimage
스위프트 3.0
let dataDecoded : Data = Data(base64Encoded: strBase64, options: .ignoreUnknownCharacters)!
let decodedimage = UIImage(data: dataDecoded)
yourImageView.image = decodedimage
목표 -C
iOS7> 버전
당신이 사용할 수있는 NSData's
base64EncodedStringWithOptions
인코딩 :
- (NSString *)encodeToBase64String:(UIImage *)image {
return [UIImagePNGRepresentation(image) base64EncodedStringWithOptions:NSDataBase64Encoding64CharacterLineLength];
}
디코딩 :
- (UIImage *)decodeBase64ToImage:(NSString *)strEncodeData {
NSData *data = [[NSData alloc]initWithBase64EncodedString:strEncodeData options:NSDataBase64DecodingIgnoreUnknownCharacters];
return [UIImage imageWithData:data];
}
iOS 6.1 및 <버전
첫 번째 옵션 : 이 링크를 사용 encode
하여 decode
이미지
에 Base64 클래스를 추가 하십시오 project
.
인코딩 :
NSData* data = UIImageJPEGRepresentation(yourImage, 1.0f);
NSString *strEncoded = [Base64 encode:data];
디코딩 :
NSData* data = [Base64 decode:strEncoded ];;
image.image = [UIImage imageWithData:data];
또 다른 옵션 : 사용 QSUtilities 에 대한 encoding
과decoding