1.ios afnetworking
NSData *imageData = UIImageJPEGRepresentation(_selectImage,0.5);
AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
[manager POST:@"https://wuxueying.xyz/jiesanbao/uploadImage.php" parameters:nil constructingBodyWithBlock:^(id<AFMultipartFormData> _Nonnull formData) {
[formData appendPartWithFileData:imageData name:@"file" fileName:@"big.jpg" mimeType:@"image/jpg"];
} success:^(NSURLSessionDataTask * _Nonnull task, id _Nonnull responseObject) {
NSLog(@"%@",responseObject);
} failure:^(NSURLSessionDataTask * _Nonnull task, NSError * _Nonnull error) {
}];
resize
- (UIImage *)imageWithImage:(UIImage *)image scaledToSize:(CGSize)newSize {
UIGraphicsBeginImageContextWithOptions(newSize, NO, 0.0);
[image drawInRect:CGRectMake(0, 0, newSize.width, newSize.height)];
UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return newImage;
}
2.php
<?php
header('Content-type: text/json; charset=UTF-8' );
if (($_FILES["file"]["type"] == "image/gif")
|| ($_FILES["file"]["type"] == "image/jpeg")
|| ($_FILES["file"]["type"] == "image/png")
|| ($_FILES["file"]["type"] == "image/pjpeg")
|| ($_FILES["file"]["type"] == "image/jpg")) {
if ($_FILES["file"]["error"] > 0) {
echo $_FILES["file"]["error"];
} else {
$fillname = $_FILES['file']['name']; // 得到文件全名
$dotArray = explode('.', $fillname); // 以.分割字符串,得到数组
$type = end($dotArray); // 得到最后一个元素:文件后缀
$path = md5(uniqid(rand())).'.'.$type; // 产生随机唯一的名字
move_uploaded_file( // 从临时目录复制到目标目录
$_FILES["file"]["tmp_name"], // 存储在服务器的文件的临时副本的名称
"images/".$path);
$str = array("retcode" => "1","imageUrl" => $path);
echo json_encode(array("url" => $path));
}
} else {
}
?>
3. Postscript
any question comment below,i will response you.