用AFNetworking上傳圖片

來源:互聯網
上載者:User

標籤:

其實我弄了好久,也沒弄好多張一起上傳,只好弄單張上傳,如果你看到這篇文章對您有協助的話歡迎您及時完善我的資訊,也讓我學習一下多張上傳怎麼弄,謝謝

//首先執行個體化一個AFHTTPRequestOperationManager操作對象

  AFHTTPRequestOperationManager *manager = [[AFHTTPRequestOperationManager alloc] initWithBaseURL:[NSURL URLWithString:@"上傳圖片的介面"]];
    
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask, YES);
    NSString *documentsDirectory = [[paths objectAtIndex:0] stringByAppendingPathComponent:@"logo1.jpg"];
    NSData *logoJpg = [NSData dataWithContentsOfFile:documentsDirectory];
   UIImage * image = [UIImage imageWithData:logoJpg];

上面是從手機路徑中取圖片
    //    NSURL *filePath = [NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:@"avatar.jpg" ofType:nil]];
    
    NSData *imageData = UIImageJPEGRepresentation(image, 1.0);//壓縮
   

 UIImage *image = [UIImage imageNamed:@"頭像1"];      NSData *data = UIImagePNGRepresentation(image);                  // 在網路開發中,上傳檔案時,是檔案不允許被覆蓋,檔案重名        // 要解決此問題,         // 可以在上傳時使用當前的系統事件作為檔案名稱         NSDateFormatter *formatter = [[NSDateFormatter alloc] init];         // 設定時間格式         formatter.dateFormat = @"yyyyMMddHHmmss";         NSString *str = [formatter stringFromDate:[NSDate date]];         NSString *fileName = [NSString stringWithFormat:@"%@.png", str];          /*         此方法參數         1. 要上傳的[位元據]         2. 對應網站上[upload.php中]處理檔案的[欄位"file"]          3. 要儲存在伺服器上的[檔案名稱]         4. 上傳檔案的[mimeType]          */

    if (imageData) {
        NSDictionary * dic1 = @{};//介面要求的參數
        NSMutableURLRequest *request = [manager.requestSerializer
                                        multipartFormRequestWithMethod:@"POST"
                                        URLString:@"http://yapi.chelehuo.net/upload"
                                        parameters:dic1
                                        constructingBodyWithBlock:^(id<AFMultipartFormData> formData) {
                                            
                                            [formData appendPartWithFileData:imageData
                                                                        name:@"logo1"
                                                                    fileName:@"logo1.jpg"
                                                                    mimeType:@"image/jpeg"];
                                            
                                        }
                                        error:nil];
        
        AFHTTPRequestOperation *operation = [manager HTTPRequestOperationWithRequest:request success:^(AFHTTPRequestOperation *operation, id responseObject) {
            NSLog(@"operation1:%@", responseObject);
           弄你想要的參數
        } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
            NSLog(@"failure. upload....");
        }];
        [manager.operationQueue addOperation:operation];
        //可以查看上傳的進度
        [operation setUploadProgressBlock:^(NSUInteger bytesWritten, long long totalBytesWritten, long long totalBytesExpectedToWrite) {
            
            NSLog(@"百分比:%f",totalBytesWritten*1.0/totalBytesExpectedToWrite);
            
        }];
    }

多張圖片上傳的方法

方法一:

NSMutableURLRequest *request = [[AFHTTPRequestSerializer serializer]multipartFormRequestWithMethod:@"POST" URLString:url parameters:url參數 constructingBodyWithBlock:^(id<AFMultipartFormData> formData) {

        for (int i = 0; i<arrayImage.count; i++) {

            UIImage *uploadImage = arrayImage[i];

            [formData appendPartWithFileData:UIImagePNGRepresentation(uploadImage)name:[NSString stringWithFormat:@"參數%d",i+1] fileName:@"test.jpg"mimeType:@"image/jpg"];

        }

    } error:nil];


AFHTTPRequestOperation *opration = [[AFHTTPRequestOperationalloc]initWithRequest:request];   

opration.responseSerializer.acceptableContentTypes = [NSSetsetWithObject:@"text/html"];

[opration setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, idresponseObject) {


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


}

這個多張上傳我是失敗的不知道為啥

這是我在網上看到的幾個部落格裡的例子,可以看看

http://blog.csdn.net/liuwuguigui/article/details/40142739

http://blog.sina.com.cn/s/blog_a776fc650102uyvb.html

http://www.tuicool.com/articles/JFJrue

用AFNetworking上傳圖片

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.