Api 接口返回 base64 圖片字符串,需要顯示在UIImageView 上。
假設 string類型的 base64ImageStr 為 api返回的 base64字符串
將base64字符串進行處理
//去除掉首尾的空白字符和換行字符
NSString * img64 = [img stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
//進行空字符串的處理
img64 = [img64 stringByReplacingOccurrencesOfString:@"\r" withString:@""];
//進行換行字符串的處理
img64 = [img64 stringByReplacingOccurrencesOfString:@"\n" withString:@""];
//進行字符串轉data數據
NSData * decodedImgData = [[NSData alloc] initWithBase64EncodedString:img64 options:NSDataBase64DecodingIgnoreUnknownCharacters];
//把data數據轉換成圖片內容
weakSelf.picCodeImageView.image = [UIImage imageWithData:decodedImgData];
//去除掉首尾的空白字符和換行字符
? ? ? ? ? ? NSString * img64 = [base64ImageStr stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
? ? ? ? ? ? //進行空字符串的處理
? ? ? ? ? ? img64 = [img64 stringByReplacingOccurrencesOfString:@"\r" withString:@""];
? ? ? ? ? ? //進行換行字符串的處理
? ? ? ? ? ? img64 = [img64 stringByReplacingOccurrencesOfString:@"\n" withString:@""];
?? ? ? ? ? ?
? ? ? ? ? ? //進行字符串轉data數據
? ? ? ? ? ? NSData * decodedImgData = [[NSData alloc] initWithBase64EncodedString:img64 options:NSDataBase64DecodingIgnoreUnknownCharacters];
?? ? ? ? ? ?
? ? ? ? ? ? //把data數據轉換成圖片內容文章來源:http://www.zghlxwxcb.cn/news/detail-801344.html
? ? ? ? ? ? weakSelf.picCodeImageView.image = [UIImage imageWithData:decodedImgData];文章來源地址http://www.zghlxwxcb.cn/news/detail-801344.html
到了這里,關于iOS base64 轉 data |圖片Base64轉NSData | UIImageView | UIImage的文章就介紹完了。如果您還想了解更多內容,請在右上角搜索TOY模板網以前的文章或繼續(xù)瀏覽下面的相關文章,希望大家以后多多支持TOY模板網!