如何使用AFNetworking进行上传图片

如何使用AFNetworking进行上传图片
首先导入头文件

#import "AFHTTPClient.h"

#import “AFHTTPRequestOperation.h"

导入 MobileCoreServices.framwork

@interface ****

{

    NSOperationQueue *_queue;

}

具体实现

 NSURL *url = [NSURL URLWithString:@"http://182.92.104.226:8080];//这个地方是后台要传得服务器器地址

    

    AFHTTPClient *httpClient = [[AFHTTPClient allocinitWithBaseURL:url];

    

    NSData *imageData = UIImageJPEGRepresentation(_im0.5);//这个地方要问你后台支持什么格式的图片,我们后台要JPEG得

    

    NSMutableURLRequest *request = [httpClient multipartFormRequestWithMethod:@"POST" path:@"/apitest/upload" parameters:nil constructingBodyWithBlock: ^(id <AFMultipartFormData>formData) {//第一个参数是POST请求,第二个是 服务器的路径  第三个是服务器需要参数的字典

        

        [formData appendPartWithFileData:imageData name:@"avatar" fileName:@"avatar.jpg" mimeType:@"image/jpeg];//给你的图片往服务器发送并起名字

        

    }];

    AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation allocinitWithRequest:request];

    

    [operation setUploadProgressBlock:^(NSUInteger bytesWritten, long long totalBytesWritten, long long totalBytesExpectedToWrite) {

        

        NSLog(@"Sent %lld of %lld bytes", totalBytesWritten, totalBytesExpectedToWrite);

        

    }];

    

    [operation start];

    

    [operation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject)

     {

         NSLog(@"成功呢!json ===%@",operation.responseString);

     } failure:^(AFHTTPRequestOperation *operation, NSError *error) {

         NSLog(@"Failure: %@", error);

     }];//监测系统的返回值



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值