使用AFN框架发送图片

来源:互联网 发布:淘宝一元商品怎么赚钱 编辑:程序博客网 时间:2024/05/22 00:14
/** *  发送带图片的微博 */- (void)sendWithImage{    //1.创建请求管理对象    AFHTTPRequestOperationManager *mgr = [AFHTTPRequestOperationManager manager];        //2.封装请求参数    NSMutableDictionary *params = [NSMutableDictionary dictionary];    params[@"status"] = self.textView.text;    params[@"access_token"] = [AccountTool account].access_token;        //3.发送    [mgr POST:@"https://upload.api.weibo.com/2/statuses/upload.json" parameters:params constructingBodyWithBlock:^(id<AFMultipartFormData> formData) {        //必须在这里说明要上传那些文件        NSData *data = UIImageJPEGRepresentation(self.imageView.image, 0.2);        [formData appendPartWithFileData:data name:@"pic" fileName:@"1.jpg" mimeType:@"image/jpeg"];    }  success:^(AFHTTPRequestOperation *operation, id responseObject) {                [MBProgressHUD showSuccess:@"发送成功"];            } failure:^(AFHTTPRequestOperation *operation, NSError *error) {                [MBProgressHUD showError:@"发送失败"];    }];}

处理多张图片的情况:

 //1.创建请求管理对象    AFHTTPRequestOperationManager *mgr = [AFHTTPRequestOperationManager manager];        //2.封装请求参数    NSMutableDictionary *params = [NSMutableDictionary dictionary];    params[@"status"] = self.textView.text;    params[@"access_token"] = [AccountTool account].access_token;        //3.发送    [mgr POST:@"https://upload.api.weibo.com/2/statuses/upload.json" parameters:params constructingBodyWithBlock:^(id<AFMultipartFormData> formData) {        //必须在这里说明要上传那些文件        NSArray *images = [self.photosView totalImages];        int count = 0;        for (UIImage *image in images) {            count++;            NSData *data = UIImageJPEGRepresentation(image, 0.2);            NSString *fileName = [NSString stringWithFormat:@"%d.jpg",count];            [formData appendPartWithFileData:data name:@"pic" fileName:fileName mimeType:@"image/jpeg"];        }    }  success:^(AFHTTPRequestOperation *operation, id responseObject) {                [MBProgressHUD showSuccess:@"发送成功"];            } failure:^(AFHTTPRequestOperation *operation, NSError *error) {                [MBProgressHUD showError:@"发送失败"];    }];


0 0
原创粉丝点击