//思路如下
1、将image转成NSData
2、通过传递参数的形式 而不是formData的方式进行传输,formData(有时可以接受到,有时候不行,不稳定)
3、php获取二进制数据 进行存储。
//代码如下
ios端
-(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 formData) {
} success:^(AFHTTPRequestOperation *operation, id responseObject) {
NSLog(@"Success: %@", responseObject);
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
NSLog(@"Error: %@", error);
}];
}
php端
public function upload(){
$data=$_POST['file'];
$filename=time().".jpg";
$file=ROOT."/log/".$filename;
$handle=fopen($file,"w+");
fwrite($handle,$data);
fclose($handle);
}
php端判断二进制流文件格式(不是必须的)
/**
* 通过二进制流获取文件类型
* @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;
}
//你可能还需要等比例压缩图片 在上传前处理 不然数据流量太大了
-(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
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.cnblogs.com/yswdarren/p/3611934.html