文件流的方式上传

时间:2022-08-15 23:52:23

先把图片转换成二进制格式

 NSData *imageData = UIImageJPEGRepresentation(imgHead, 1.0);

//    NSData *imageData = UIImagePNGRepresentation(headImg);
    
    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
    manager.responseSerializer = [AFHTTPResponseSerializer serializer];
    manager.requestSerializer.timeoutInterval = 15;
    NSString *urlStr = [NSString stringWithFormat:@"http://b2c.ezparking.com.cn/rtpi-service/member/updateIcon.do?key=%@&id=%@",reqKey,userid];
    [manager POST:urlStr parameters:nil constructingBodyWithBlock:^(id<AFMultipartFormData> formData) {
        NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
        formatter.dateFormat = @"yyyyMMddHHmmss";
        NSString *str = [formatter stringFromDate:[NSDate date]];
        NSString *fileName = [NSString stringWithFormat:@"%@%@.jpg", str,userid];
        
        // 上传图片,以文件流的格式
        [formData appendPartWithFileData:imageData name:@"icon" fileName:fileName mimeType:@"image/jpeg"];
    } success:^(AFHTTPRequestOperation * operation, id retObject) {
        LogPark(@"head suceess:%@",retObject);
        [self hideHud];
    } failure:^(AFHTTPRequestOperation * operation, NSError * error) {
        LogPark(@"head failure:%@",error);
        [self hideHud];
        NSString *retStr = [PublicMethod failureCode:operation.response.statusCode dictionary:operation.response.allHeaderFields];
        [self showHint:retStr yOffset:-150];
    }];


多张图片上传:

[manager POST:url parameters:userData constructingBodyWithBlock:^(id<AFMultipartFormData>  _Nonnull formData) {
            for (int i = 0; i<self.imageArray.count-1; i++) {
                FSImageModel *model = self.imageArray[i];
                UIImage *image = [UIImage imageWithData:model.data];
                NSData * imageData = UIImagePNGRepresentation(image);
                NSDate *  nowNate=[NSDate date];
                NSString * fileName = [NSString stringWithFormat:@"%@%@.png",model.assetUrl,nowNate];
                [formData appendPartWithFileData:imageData name:@"orders.files"fileName:fileName mimeType:@"image/png"];
            }
            
            
            
        }


图片处理:

- (UIImage*)imageWithImageSimple:(UIImage*)image scaledToSize:(CGSize)newSize
{
    // Create a graphics image context
    UIGraphicsBeginImageContext(newSize);
    
    // Tell the old image to draw in this new context, with the desired
    // new size
    [image drawInRect:CGRectMake(0,0,newSize.width,newSize.height)];
    
    // Get the new image from the context
    UIImage* newImage = UIGraphicsGetImageFromCurrentImageContext();
    
    // End the context
    UIGraphicsEndImageContext();
    
    // Return the new image.
    return newImage;
}

视频音频上传

主要还是看你服务器怎么接受的,一般都是用流的方式上传,就跟你传文件一样。

可以用表单的方式,也可以直接把你的音频文件作为body,直接上传,只要你服务器可以知道你传的是什么就ok。