//思路以下php
一、將image轉成NSDatahtml
二、經過傳遞參數的形式 而不是formData的方式進行傳輸,formData(有時能夠接受到,有時候不行,不穩定)java
三、php獲取二進制數據 進行存儲。ios
//代碼以下.net
ios端code
-(void)uploadImage:(UIImage *)image{ AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager]; NSData * imageData=UIImageJPEGRepresentation(image, 0.1);//轉換成二進制的數據流 NSDictionary *parameters = @{@"file": imageData};//這一步是重點 [manager POST:@"http://www.d-shang.com/index.php?blog/upload" parameters:parameters constructingBodyWithBlock:^(id<AFMultipartFormData> formData) { } success:^(AFHTTPRequestOperation *operation, id responseObject) { NSLog(@"Success: %@", responseObject); } failure:^(AFHTTPRequestOperation *operation, NSError *error) { NSLog(@"Error: %@", error); }]; }
php端orm
public function upload(){ $data=$_POST['file']; $filename=time().".jpg"; $file=ROOT."/log/".$filename; $handle=fopen($file,"w+"); fwrite($handle,$data); fclose($handle); }
php端判斷二進制流文件格式(不是必須的)htm
/** * 經過二進制流獲取文件類型 * @param string $binary_data * @return string */ function get_file_type_binary_data($binary_data){ $str_info = @unpack("c2chars", substr($binary_data, 0, 2)); $type_code = $str_info['chars1'].$str_info['chars2']; switch ($type_code) { case '7790': $file_type = 'exe'; break; case '7784': $file_type = 'midi'; break; case '8075': $file_type = 'zip'; break; case '8297': $file_type = 'rar'; break; case '255216': case '-1-40': $file_type = 'jpg'; break; case '7173': $file_type = 'gif'; break; case '6677': $file_type = 'bmp'; break; case '13780': case '-11980': $file_type = 'png'; break; default: $file_type = 'unknown'; break; } return $file_type; }
//你可能還須要等比例壓縮圖片 在上傳前處理 否則數據流量太大了blog
-(UIImage *) imageCompressForWidth:(UIImage *)sourceImage targetWidth:(CGFloat)defineWidth{ UIImage *newImage = nil; CGSize imageSize = sourceImage.size; CGFloat width = imageSize.width; CGFloat height = imageSize.height; CGFloat targetWidth = defineWidth; CGFloat targetHeight = height / (width / targetWidth); CGSize size = CGSizeMake(targetWidth, targetHeight); CGFloat scaleFactor = 0.0; CGFloat scaledWidth = targetWidth; CGFloat scaledHeight = targetHeight; CGPoint thumbnailPoint = CGPointMake(0.0, 0.0); if(CGSizeEqualToSize(imageSize, size) == NO){ CGFloat widthFactor = targetWidth / width; CGFloat heightFactor = targetHeight / height; if(widthFactor > heightFactor){ scaleFactor = widthFactor; } else{ scaleFactor = heightFactor; } scaledWidth = width * scaleFactor; scaledHeight = height * scaleFactor; if(widthFactor > heightFactor){ thumbnailPoint.y = (targetHeight - scaledHeight) * 0.5; }else if(widthFactor < heightFactor){ thumbnailPoint.x = (targetWidth - scaledWidth) * 0.5; } } UIGraphicsBeginImageContext(size); CGRect thumbnailRect = CGRectZero; thumbnailRect.origin = thumbnailPoint; thumbnailRect.size.width = scaledWidth; thumbnailRect.size.height = scaledHeight; [sourceImage drawInRect:thumbnailRect]; newImage = UIGraphicsGetImageFromCurrentImageContext(); if(newImage == nil){ NSLog(@"scale image fail"); } UIGraphicsEndImageContext(); return newImage; }
參考資料:圖片
[上傳用法]http://www.blogjava.net/qileilove/archive/2014/12/11/421323.html
[等比例縮小處理]http://www.cnblogs.com/yswdarren/p/3611934.html