服務器請求限制:
iis默認只有200k
跟后臺反應一下。iis默認200K限制。ngnix默認1M,apache默認2M。
圖片壓縮:(寫了一個分類方法)
@implementation UIImage (Compress)
/*
*? 壓縮圖片至目標尺寸
*
*? @param targetWidth 圖片最終尺寸的寬
*
*? @return 返回按照源圖片的寬、高比例壓縮至目標寬、高的圖片
*/
-(UIImage *)compressImageToTargetWidth:(CGFloat)targetWidth{
CGSize imageSize = self.size;
CGFloat width = imageSize.width;
CGFloat height = imageSize.height;
CGFloat targetHeight = (targetWidth / width) * height;
UIGraphicsBeginImageContext(CGSizeMake(targetWidth, targetHeight));
[self drawInRect:CGRectMake(0, 0, targetWidth, targetHeight)];
UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return newImage;
}
圖片轉二進制壓縮:(注意UIImageJPEGRepresentation壓縮是有限度的,當圖片比較大時,最低壓縮到200k級別,此后無論壓縮值設置多小都不能再進步壓縮)
UIImage *icon = iconArr[i][@"icon"];
NSString *name = iconArr[i][@"name"];
object.data = UIImageJPEGRepresentation(icon,0.8);//0.8為壓縮值
一般上傳多張圖片,如果圖片張數比較少,請求一次接口,將一組圖片傳給服務器。如果圖片張數比較多,用遞歸來多次請求接口,通過每次請求的返回參數(如上傳的圖片名),來判斷該圖片是否上傳成功及是否所有圖片上傳完成。
Error Domain=com.alamofire.error.serialization.response Code=-1011 "Request failed: request too large (413)" UserInfo={com.alamofire.serialization.response.error.response={ URL: http://sssy.yssp.com:80/api/Own/editChengjiao } { status code: 413, headers {
Connection = close;
"Content-Length" = 199;
"Content-Type" = "text/html";
Date = "Tue, 29 Nov 2016 02:33:02 GMT";
Server = "nginx/1.10.0";
} }, NSErrorFailingURLKey=http://ss.ysscp.com:80/api/Own/editChengjiao, com.alamofire.serialization.response.error.data=<3c68746d 6c3e0d0a 3c686561 643e3c74 69746c65 3e343133 20526571 75657374 20456e74 69747920 546f6f20 4c617267 653c2f74 69746c65 3e3c2f68 6561643e 0d0a3c62 6f647920 6267636f 6c6f723d 22776869 7465223e 0d0a3c63 656e7465 723e3c68 313e3431 33205265 71756573 7420456e 74697479 20546f6f 204c6172 67653c2f 68313e3c 2f63656e 7465723e 0d0a3c68 723e3c63 656e7465 723e6e67 696e782f 312e3130 2e303c2f 63656e74 65723e0d 0a3c2f62 6f64793e 0d0a3c2f 68746d6c 3e0d0a>, NSLocalizedDescription=Request failed: request too large (413)}