IOS開發-圖片上傳

來源:互聯網
上載者:User

標籤:後台   represent   object   home   attribute   nts   within   介面   imagej   

   目前IOS端開發,圖片上傳到伺服器分為兩種,一種是直接上到伺服器,一種是藉助第三方儲存(減少伺服器壓力)。

一、直接上傳到伺服器

 1         /** 2          *  代碼示範 3          */ 4         //*******UIImagePNGRepresentation(UIImage* image) 要比UIImageJPEGRepresentation(UIImage* image, 1.0) 返回的圖片資料量大很多,但從視角角度看,圖片的品質沒有明顯的降低,所以在讀取圖片資料內容時,可以先使用UIImageJPEGRepresentation,且耗時短。 5         //******將選中的圖片壓縮成二進位檔案*******photo為UIImage執行個體 6         NSData *data = UIImageJPEGRepresentation(photo, 0.5); 7         //****** 從本地上傳圖片  先讀入沙箱目錄 8          //****** 建立沙箱目錄 9         NSString *docomentpaths = [NSHomeDirectory() stringByAppendingPathComponent:@"docomentpaths"];10         NSFileManager *manager = [NSFileManager defaultManager];11         [manager createDirectoryAtPath:docomentpaths withIntermediateDirectories:YES attributes:nil error:nil];12         //*****擷取目前時間字元13         NSString *timeStr = [NSString stringWithFormat:@"%@",[NSDate date]];14         //*****讀入沙箱目錄下15         [manager createFileAtPath:[docomentpaths stringByAppendingString:[NSString stringWithFormat:@"%@",timeStr]] contents:data attributes:nil];16         //*****得到圖片路徑17         NSString *filepath = [[NSString alloc]initWithFormat:@"%@%@",docomentpaths,timeStr];18          //****** 得到本地的圖片URL19         NSURL *imageurl = [NSURL fileURLWithPath:filepath];20         
 1  //****url伺服器介面地址 2     //使用AFN網路架構上傳 3     self.manager = [AFHTTPRequestOperationManager manager]; 4     [self.manager POST:url parameters:dic constructingBodyWithBlock:^(id<AFMultipartFormData>  _Nonnull formData) { 5         //         開始上傳 formData提交 6            [formData appendPartWithFileURL:imageurl name:@"ios_uploadImage" fileName:filepath mimeType:@"image/jpeg" error:nil]; 7                  } 8  9         //      這裡上傳多張圖片for迴圈提交10         //        for (int i = 0; i<self.imagelist.count; i++)11         //         {12         //             [formData appendPartWithFileURL:self.iamgedata[i] name:@"heareuy" fileName:self.strlist[i] mimeType:@"image/jpeg" error:nil];13         //         }14         //15         16     } success:^(AFHTTPRequestOperation * _Nonnull operation, id  _Nonnull responseObject) {17         18     } failure:^(AFHTTPRequestOperation * _Nullable operation, NSError * _Nonnull error) {19         20     }];

二、上傳到第三方伺服器儲存-七牛雲端儲存

原來公司的項目也是上傳到公司伺服器上,但想著圖片量會很大,隨著圖片量的增多,後期也是一個問題,所以我決定使用七牛雲作為圖片儲存盤,過程其實是非常簡單的。背景夥伴們會在七牛上註冊,執行個體化一個盤,在手機端上傳時,後台會給一個從七牛擷取到token。根據這個token手機端會把圖片上傳到七牛,上傳成功後,七牛會返回一個圖片的url,然後再將這個url給伺服器,說白了,伺服器只是儲存手機端上傳到七牛的地址。

代碼示範待續。。。。。。。。。

 

IOS開發-圖片上傳

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.