在我們項目中需要將一個NSData類型的數據進行base64編碼爲一個字符串進行傳輸。這裏記錄下編碼和解碼的方式
編碼:
UIImage *image = [UIImage imageWithData:[NSData dataWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"di1" ofType:@"jpg"]]];
NSData *dataFromImage = UIImageJPEGRepresentation(image, 0.7f);
NSString *str1 = [dataFromImage base64EncodedStringWithOptions:NSDataBase64EncodingEndLineWithLineFeed];
解碼:
NSData *imagedata = [[NSData alloc] initWithBase64EncodedString:str1 options:0];