iOS 图片和视频base64编码后上传服务器


图片和视频方式相同,都是用data进行转换,所以实例中只展示了图片的代码

UIImage图片转成Base64字符串

UIImage *originImage = [UIImage imageNamed:@"icon.png"];
NSData *data = UIImageJPEGRepresentation(originImage, 1.0f);
NSString *encodedImageStr = [data base64EncodedStringWithOptions:NSDataBase64Encoding64CharacterLineLength];

Base64字符串转UIImage图片:

NSData *decodedImageData = [[NSData alloc]
                                initWithBase64EncodedString:encodedImageStr options:NSDataBase64DecodingIgnoreUnknownCharacters];
UIImage *decodedImage = [UIImage imageWithData:decodedImageData];

图片转base64使用AFNetworking 上传服务器

AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
 manager.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:
                                                                 @"application/json",
                                                                 @"text/json",
                                                                 @"text/javascript",
                                                                 @"text/html",
                                                                 @"text/plain",
                                                                 @"image/jpeg",
                                                                 @"image/png",
                                                                 @"text/plain", nil];
            manager.responseSerializer = [AFJSONResponseSerializer serializer];
            NSString *url = LC_REQUEST_URL(@"decedent/uploadFile.do");
            NSString *userId = [LCUserInfo user].userId;
            NSString *encodedImageStr = [imageData base64EncodedStringWithOptions:0];
            NSDictionary *parameters = @{@"decedentId": decedentId, @"userId": userId, @"fileType": @"image", @"files": encodedImageStr};
            [manager POST:url parameters:parameters progress:^(NSProgress * _Nonnull uploadProgress) {
                //                CGFloat progress = 100.0 * uploadProgress.completedUnitCount / uploadProgress.totalUnitCount;
                //                NSLog(@"%.2lf%%", progress);
                if (progress) {
                    progress(uploadProgress);
                }
            } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
                NSLog(@"%@", responseObject);
                if ([responseObject isKindOfClass:[NSDictionary class]]) {
                    NSInteger successCode = [responseObject[@"successCode" ] integerValue];
                    if (successCode == 1002000 && success) {
                        success(responseObject);
                    }
                }
                else {
                    if (failure) {
                        failure([NSError errorWithDomain:NSURLErrorDomain code:-1 userInfo:responseObject]);
                    }
                }

            } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
                if (failure) {
                    failure(error);
                }
            }];

附件: