#pragma mark ------- UIImage图片转成base64字符串:
//将 已转换成为 NSData 类型的图片 转换为 NSString 类型
//iOS 7.0+ ---- base64EncodedStringWithOptions
NSString *_encodedImageStr = [imageData base64EncodedStringWithOptions:NSDataBase64Encoding64CharacterLineLength];
NSLog(@"===Encoded image:\n%@", _encodedImageStr);
NSString *urlStr = [NSString stringWithFormat:kURLStr];
NSDictionary *params = @{
@"username":[NSString stringWithFormat:@"%@", [Singleton mainSingleton].telephoneData],
@"image":_encodedImageStr
};
AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
[manager POST:urlStr parameters:params constructingBodyWithBlock:^(id<AFMultipartFormData> _Nonnull formData) {
//获取存在本地沙盒的图片,前提是同一界面内做过 “将图片存在本地沙盒中” 的这一操作。
[formData appendPartWithFileURL:[NSURL fileURLWithPath:fullPath] name:@"file" fileName:@"currentImage.png" mimeType:@"image/png" error:nil];
} progress:^(NSProgress * _Nonnull uploadProgress) {
// @property int64_t totalUnitCount; 需要下载文件的总大小
// @property int64_t completedUnitCount; 当前已经下载的大小
//打印下上传进度
NSLog(@"%lf",1.0 *uploadProgress.completedUnitCount / uploadProgress.totalUnitCount);
} success:^(NSURLSessionDataTask * _Nonnull task, id _Nullable responseObject) {
//请求成功
NSLog(@"请求成功:%@",responseObject);
} failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
//请求失败
NSLog(@"请求失败:%@",error);
}];
#pragma mark ------- Base64字符串转UIImage图片:
//从网络端解析下来的字符串转换为图片显示
NSData *_decodedImageData = [[NSData alloc] initWithBase64EncodedString:[responseObject[@"items"] objectForKey:@"faceimageurl"]
options:NSDataBase64DecodingIgnoreUnknownCharacters];//NSDataBase64Encoding64CharacterLineLength
UIImage *_decodedImage = [UIImage imageWithData:_decodedImageData];
// NSLog(@"===Decoded image size: %@", NSStringFromCGSize(_decodedImage.size));
self.avatarImageV.image = _decodedImage;
PS:以上全部适用于 iOS7.0+