ios afnetworking2.x上传图片 服务端php无法接受文件问题

//思路如下

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<AFMultipartFormData> 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 < 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


转载于:https://my.oschina.net/u/554046/blog/625647

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值