码迷,mamicode.com
首页 > Web开发 > 详细

关于AFNetworking上传图片问题

时间:2015-08-13 20:15:04      阅读:152      评论:0      收藏:0      [点我收藏+]

标签:afnetworking上传图片

AFNetworking上传图片

AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];

    [manager POST:@"XXXX"  parameters:nil constructingBodyWithBlock:^(id<AFMultipartFormData> formData) {
        NSData *imageData = UIImageJPEGRepresentation(_userImg, 1);
        
        // 上传图片,以文件流的格式 name 对应网站上[upload.php中]处理文件的[字段"file"] fileName 不能为空且必须带后缀,如:test.jpg
        [formData appendPartWithFileData:imageData name:@"photo" fileName:@"XXXX.jpg" mimeType:@"image/jpeg"];

     } success:^(AFHTTPRequestOperation *operation, id responseObject) {
        [_activityView stopAnimating];
        if ([[responseObject objectForKey:@"success"] intValue] == YES) {
            [self makeToast:@"头像修改成功!" duration:2.0 position:@"bottom"];
            [self saveUserInfo:[responseObject objectForKey:@"data"] key:@"avatar"];
        }else{
            id data = [responseObject objectForKey:@"data"];
            [self makeToast:[data objectForKey:@"message"] duration:2.0 position:@"bottom"];
        }
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        [_activityView stopAnimating];
        [self makeToast:@"网络不给力!" duration:2.0 position:@"bottom"];
    }];

特别注意注释行!

版权声明:本文为博主原创文章,未经博主允许不得转载。

关于AFNetworking上传图片问题

标签:afnetworking上传图片

原文地址:http://blog.csdn.net/gyz413977349/article/details/47617027

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!