新浪微博客户端(40)-使用AFN发送带图片的微博

 

DJComposeViewController.m

/** 发微博 */
- (void)sendStatusRequest {

    AFHTTPSessionManager *RequestManager = [AFHTTPSessionManager manager];
    
    NSArray *photos = self.photosView.photos;
    if (photos.count) { // 当前用户选择了图片
        
        UIImage *photoImage = [photos firstObject];
        NSString *urlString = @"https://upload.api.weibo.com/2/statuses/upload.json";
        NSMutableDictionary *params = [NSMutableDictionary dictionary];
        params[@"access_token"] = [DJAccountTool account].access_token;
        params[@"status"] = self.textView.text;
        
        [RequestManager POST:urlString parameters:params constructingBodyWithBlock:^(id<AFMultipartFormData>  _Nonnull formData) {
            [formData appendPartWithFileData:UIImageJPEGRepresentation(photoImage, 1.0) name:@"pic" fileName:@"upload.jg" mimeType:@"image/jpeg"];
        } progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
            [MBProgressHUD showSuccess:@"发送成功"];
        } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
            [MBProgressHUD showError:@"发送失败"];
        }];
        
        
    } else { // 当前用户未选择图片
        
        NSString *urlString = @"https://api.weibo.com/2/statuses/update.json";
        
        NSMutableDictionary *params = [NSMutableDictionary dictionary];
        params[@"access_token"] = [DJAccountTool account].access_token;
        params[@"status"] = self.textView.text;
        
        [RequestManager POST:urlString parameters:params progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
            [MBProgressHUD showSuccess:@"发送成功"];
        } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
            [MBProgressHUD showError:@"发送失败"];
        }];
    
    }
    
    // 关闭当前界面
    [self dismissViewControllerAnimated:YES completion:nil];
}

最终效果:

 

  

posted @ 2016-11-26 20:42  夜行过客  阅读(256)  评论(0编辑  收藏  举报