上传图片到服务器 对图片进行压缩
2017-07-26 10:40
417 查看
iPhone拍照 图片还是有点大 对图片不进行处理 上传到服务器 经查询一张图片大小约1.8M左右
因为一次性可能要上传几十张图片 1.8M一张图片 这明显有点大了
忘记从哪位网友那看来的,不过我项目中使用了,经过处理后图片一张大约200k吧
图片压缩代码
.h
#import <UIKit/UIKit.h>
@interface UIImage (lxy)
- (UIImage *)scaleToSize:(UIImage *)img size:(CGSize)size;
- (UIImage *)imageByScalingAndCroppingForSize:(CGSize)targetSize;
@end
.m
#import "UIImage+lxy.h"
@implementation UIImage (lxy)
- (UIImage *)scaleToSize:(UIImage *)img size:(CGSize)size{
// 创建一个bitmap的context
// 并把它设置成为当前正在使用的context
UIGraphicsBeginImageContext(size);
// 绘制改变大小的图片
[img drawInRect:CGRectMake(0, 0, size.width, size.height)];
// 从当前context中创建一个改变大小后的图片
UIImage* scaledImage = UIGraphicsGetImageFromCurrentImageContext();
// 使当前的context出堆栈
UIGraphicsEndImageContext();
// 返回新的改变大小后的图片
return scaledImage;
}
- (UIImage*)imageByScalingAndCroppingForSize:(CGSize)targetSize
{
UIImage *sourceImage = self;
UIImage *newImage = nil;
CGSize imageSize = sourceImage.size;
CGFloat width = imageSize.width;
CGFloat height = imageSize.height;
CGFloat targetWidth = targetSize.width;
CGFloat targetHeight = targetSize.height;
CGFloat scaleFactor = 0.0;
CGFloat scaledWidth = targetWidth;
CGFloat scaledHeight = targetHeight;
CGPoint thumbnailPoint = CGPointMake(0.0,0.0);
if (CGSizeEqualToSize(imageSize, targetSize) == NO)
{
CGFloat widthFactor = targetWidth / width;
CGFloat heightFactor = targetHeight / height;
if (widthFactor > heightFactor)
scaleFactor = widthFactor; // scale to fit height
else
scaleFactor = heightFactor; // scale to fit width
scaledWidth = width * scaleFactor;
scaledHeight = height * scaleFactor;
// center the image
if (widthFactor > heightFactor)
{
thumbnailPoint.y = (targetHeight - scaledHeight) * 0.5;
}
else
if (widthFactor < heightFactor)
{
thumbnailPoint.x = (targetWidth - scaledWidth) * 0.5;
}
}
UIGraphicsBeginImageContext(targetSize); // this will crop
CGRect thumbnailRect = CGRectZero;
thumbnailRect.origin = thumbnailPoint;
thumbnailRect.size.width = scaledWidth;
thumbnailRect.size.height = scaledHeight;
[sourceImage drawInRect:thumbnailRect];
newImage = UIGraphicsGetImageFromCurrentImageContext();
if(newImage == nil){
}
// NSLog(@"could not scale image");
//pop the context to get back to the default
UIGraphicsEndImageContext();
return newImage;
}
我在项目中的使用方法
#pragma mark---ImagePickerControllerDelegate---
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary<NSString *,id> *)info{
UIImage *image = [info valueForKey:UIImagePickerControllerOriginalImage];
//通过判断picker的sourceType,如果是拍照则保存到相册去
if (picker.sourceType == UIImagePickerControllerSourceTypeCamera) {
UIImageWriteToSavedPhotosAlbum(image, self, @selector(image:didFinishSavingWithError:contextInfo:), nil);
}
CGSize size = image.size;
if (size.width > 800) {
size.height = size.height*800/size.width;
size.width = 800;
} else if (size.width < 500) {
size.width = 500;
size.height = size.height;
}
image = [[[UIImage alloc]init]scaleToSize:image size:size];
SafeCheck *model = _dataArray[subNumber];
[model.imageList addObject:image];
_dataArray[subNumber] = model;
NSString *title = model.imageList.count?[NSString stringWithFormat:@"已拍:%ld",(long)model.imageList.count]:@"拍照";
[((SafeCheckCell *)[_mainTBV cellForRowAtIndexPath:[NSIndexPath indexPathForRow:subNumber inSection:0]]).photoBtn setTitle:title forState:UIControlStateNormal];
[picker dismissViewControllerAnimated:NO completion:nil];
}
- (void)image:(UIImage *)image didFinishSavingWithError:(NSError *)error contextInfo:(void *)contextInfo {
}
因为一次性可能要上传几十张图片 1.8M一张图片 这明显有点大了
忘记从哪位网友那看来的,不过我项目中使用了,经过处理后图片一张大约200k吧
图片压缩代码
.h
#import <UIKit/UIKit.h>
@interface UIImage (lxy)
- (UIImage *)scaleToSize:(UIImage *)img size:(CGSize)size;
- (UIImage *)imageByScalingAndCroppingForSize:(CGSize)targetSize;
@end
.m
#import "UIImage+lxy.h"
@implementation UIImage (lxy)
- (UIImage *)scaleToSize:(UIImage *)img size:(CGSize)size{
// 创建一个bitmap的context
// 并把它设置成为当前正在使用的context
UIGraphicsBeginImageContext(size);
// 绘制改变大小的图片
[img drawInRect:CGRectMake(0, 0, size.width, size.height)];
// 从当前context中创建一个改变大小后的图片
UIImage* scaledImage = UIGraphicsGetImageFromCurrentImageContext();
// 使当前的context出堆栈
UIGraphicsEndImageContext();
// 返回新的改变大小后的图片
return scaledImage;
}
- (UIImage*)imageByScalingAndCroppingForSize:(CGSize)targetSize
{
UIImage *sourceImage = self;
UIImage *newImage = nil;
CGSize imageSize = sourceImage.size;
CGFloat width = imageSize.width;
CGFloat height = imageSize.height;
CGFloat targetWidth = targetSize.width;
CGFloat targetHeight = targetSize.height;
CGFloat scaleFactor = 0.0;
CGFloat scaledWidth = targetWidth;
CGFloat scaledHeight = targetHeight;
CGPoint thumbnailPoint = CGPointMake(0.0,0.0);
if (CGSizeEqualToSize(imageSize, targetSize) == NO)
{
CGFloat widthFactor = targetWidth / width;
CGFloat heightFactor = targetHeight / height;
if (widthFactor > heightFactor)
scaleFactor = widthFactor; // scale to fit height
else
scaleFactor = heightFactor; // scale to fit width
scaledWidth = width * scaleFactor;
scaledHeight = height * scaleFactor;
// center the image
if (widthFactor > heightFactor)
{
thumbnailPoint.y = (targetHeight - scaledHeight) * 0.5;
}
else
if (widthFactor < heightFactor)
{
thumbnailPoint.x = (targetWidth - scaledWidth) * 0.5;
}
}
UIGraphicsBeginImageContext(targetSize); // this will crop
CGRect thumbnailRect = CGRectZero;
thumbnailRect.origin = thumbnailPoint;
thumbnailRect.size.width = scaledWidth;
thumbnailRect.size.height = scaledHeight;
[sourceImage drawInRect:thumbnailRect];
newImage = UIGraphicsGetImageFromCurrentImageContext();
if(newImage == nil){
}
// NSLog(@"could not scale image");
//pop the context to get back to the default
UIGraphicsEndImageContext();
return newImage;
}
我在项目中的使用方法
#pragma mark---ImagePickerControllerDelegate---
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary<NSString *,id> *)info{
UIImage *image = [info valueForKey:UIImagePickerControllerOriginalImage];
//通过判断picker的sourceType,如果是拍照则保存到相册去
if (picker.sourceType == UIImagePickerControllerSourceTypeCamera) {
UIImageWriteToSavedPhotosAlbum(image, self, @selector(image:didFinishSavingWithError:contextInfo:), nil);
}
CGSize size = image.size;
if (size.width > 800) {
size.height = size.height*800/size.width;
size.width = 800;
} else if (size.width < 500) {
size.width = 500;
size.height = size.height;
}
image = [[[UIImage alloc]init]scaleToSize:image size:size];
SafeCheck *model = _dataArray[subNumber];
[model.imageList addObject:image];
_dataArray[subNumber] = model;
NSString *title = model.imageList.count?[NSString stringWithFormat:@"已拍:%ld",(long)model.imageList.count]:@"拍照";
[((SafeCheckCell *)[_mainTBV cellForRowAtIndexPath:[NSIndexPath indexPathForRow:subNumber inSection:0]]).photoBtn setTitle:title forState:UIControlStateNormal];
[picker dismissViewControllerAnimated:NO completion:nil];
}
- (void)image:(UIImage *)image didFinishSavingWithError:(NSError *)error contextInfo:(void *)contextInfo {
}
相关文章推荐
- ios中摄像头/相册获取图片,压缩图片,上传服务器方法总结
- ios中摄像头/相册获取图片,压缩图片,上传服务器方法总结
- ios中摄像头/相册获取图片,压缩图片,上传服务器方法总结
- vue之使用Cropper进行图片剪裁上传压缩
- php实现上传图片并进行压缩,之后覆盖原图片
- ios中摄像头/相册获取图片,压缩图片,上传服务器方法总结
- html使用canvas进行图片压缩上传 (废话不说直接贴代码,不懂的话在讨论)
- 前台使用ajax上传图片,后台 node.js 使用 formidable 进行图片接受并存于服务器静态文件夹中
- kindeditor 隐藏网络图片功能,只保留本地上传功能,并且将图片进行压缩处理
- HTML转PDF,再转图片、影像文件,并压缩及上传到ftp服务器
- 上传图片图像进行压缩后上传
- ios中摄像头/相册获取图片,压缩图片,上传服务器方法总结
- ios中摄像头/相册获取图片,压缩图片,上传服务器方法总结
- kindeditor 隐藏网络图片功能,只保留本地上传功能,并且将图片进行压缩处理
- 压缩本地图片并上传至服务器
- ios中摄像头/相册获取图片,压缩图片,上传服务器方法总结
- 前端js+后端php进行图片压缩上传(解决move_uploaded_file失败并附源码)
- 微信公众号开发《四》使用微信JS-SDK实现手机图片上传,支持压缩、预览。并下载图片到自身服务器
- ios中摄像头/相册获取图片,压缩图片,上传服务器方法总结
- 【iphone开发】从摄像头/相册获取图片,压缩图片,上传服务器小结