iOS 上传图片到服务器

1.单张图片上传

//上传图片(单张)
+(void)uploadPhotoAndController:(UIViewController *)controller WithSize:(CGSize)size Image:(UIImage*)image AndFinish:(void (^)(NSDictionary *, NSError *))finish
{
    //加载提示菊花
    MBProgressHUD *hud;
    if(controller){
        hud = [MBProgressHUD showHUDAddedTo:controller.view animated:YES];
        hud.label.text = NSLocalizedString(@"加载中...", @"HUD loading title");
    }
 
    //1. 存放图片的服务器地址,这里我用的宏定义
    NSString * url = [NSString stringWithFormat:@"%@%@",Hx_Main_heard_API,IMAGE_UPLOAD_URL_API];
    
    //2. 利用时间戳当做图片名字
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];  
    formatter.dateFormat = @"yyyyMMddHHmmss";  
    NSString *imageName = [formatter stringFromDate:[NSDate date]];  
    NSString *fileName = [NSString stringWithFormat:@"%@.jpg",imageName];
    
    //3. 图片二进制文件
    NSData *imageData = UIImageJPEGRepresentation(image, 0.7f);
    NSLog(@"upload image size: %ld k", (long)(imageData.length / 1024));
    
    //4. 发起网络请求
    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
    manager.requestSerializer = [AFHTTPRequestSerializer serializer];
    manager.responseSerializer = [AFJSONResponseSerializer serializer];
    [manager POST:url parameters:nil constructingBodyWithBlock:^(id formData) {
        // 上传图片,以文件流的格式,这里注意:name是指服务器端的文件夹名字
        [formData appendPartWithFileData:imageData name:@"file" fileName:fileName mimeType:@"image/jpeg"];
    } success:^(AFHTTPRequestOperation *operation, id responseObject) {
        //上传成功时的回调
        [hud hideAnimated:YES];
        NSLog(@"%@",responseObject);
        finish(responseObject,nil);
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        //失败时的回调
        [hud hideAnimated:YES];
        finish(nil,error);
    }];
}

2.多张图片上传

// 上传图片(多张)
+(void)uploadPhotoAndController:(UIViewController *)controller WithSize:(CGSize)size Image:(UIImage*)image AndFinish:(void (^)(NSDictionary *, NSError *))finish
{
    //加载提示菊花
    MBProgressHUD *hud;
    if(controller){
        hud = [MBProgressHUD showHUDAddedTo:controller.view animated:YES];
        hud.label.text = NSLocalizedString(@"加载中...", @"HUD loading title");
    }
 
    //1. 存放图片的服务器地址,这里我用的宏定义
    NSString * url = [NSString stringWithFormat:@"%@%@",Hx_Main_heard_API,IMAGE_UPLOAD_URL_API];
    
    //2. 发起网络请求
    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
    manager.requestSerializer = [AFHTTPRequestSerializer serializer];
    manager.responseSerializer = [AFJSONResponseSerializer serializer];    
    [manager POST:url parameters:nil constructingBodyWithBlock:^(id formData)
    {
        // 上传多张图片
        for(NSInteger i = 0; i < self.imageDataArray.count; i++)
        {
            //取出单张图片二进制数据
            NSData * imageData = self.imageDataArray[i];
            
            // 上传的参数名,在服务器端保存文件的文件夹名
            NSString * Name = [NSString stringWithFormat:@"%@%ld", Image_Name, i+1];
            // 上传filename
            NSString * fileName = [NSString stringWithFormat:@"%@.jpg", Name];
            
            [formData appendPartWithFileData:imageData name:Name fileName:fileName mimeType:@"image/jpeg"];
        }
    }
    success:^(AFHTTPRequestOperation *operation, id responseObject)
     {
        //上传成功时的回调
        [hud hideAnimated:YES];
        NSLog(@"%@",responseObject);
        finish(responseObject,nil);           
     }
     failure:^(AFHTTPRequestOperation *operation, NSError *error)
     {
        //失败时的回调
        [hud hideAnimated:YES];
        finish(nil,error);
     }];
}

PS:上传图片前,必须先压缩图片,不然图片过大,可能会导致上传失败!

你可能感兴趣的:(iOS 上传图片到服务器)