符合中小企业对网站设计、功能常规化式的企业展示型网站建设
本套餐主要针对企业品牌型网站、中高端设计、前端互动体验...
商城网站建设因基本功能的需求不同费用上面也有很大的差别...
手机微信网站开发、微信官网、微信商城网站...
在项目中经常遇到要上传图片,如果直接上传,那么会上传比较大的图片,导致费流量,刷新时加载图片时间过长,手机内存占用率高等问题。
创新互联专业为企业提供让胡路网站建设、让胡路做网站、让胡路网站设计、让胡路网站制作等企业网站建设、网页设计与制作、让胡路企业网站模板建站服务,10余年让胡路做网站经验,不只是建网站,更提供有价值的思路和整体网络服务。
一、先来介绍下概念:
图片的压缩其实是俩概念,
1、是 “压” 文件体积变小,但是像素数不变,长宽尺寸不变,那么质量可能下降,
2、是 “缩” 文件的尺寸变小,也就是像素数减少。长宽尺寸变小,文件体积同样会减小。
二、解决方法(以上传头像为例),先缩再压:
2.1 矫正图片方向(照片是有方向的,避免出现“倒立”的情况)
- (UIImage*)fixOrientation:(UIImage*)aImage {
// No-op if the orientation is already correct
if(aImage.imageOrientation==UIImageOrientationUp)
returnaImage;
// We need to calculate the proper transformation to make the image upright.
// We do it in 2 steps: Rotate if Left/Right/Down, and then flip if Mirrored.
CGAffineTransformtransform =CGAffineTransformIdentity;
switch(aImage.imageOrientation) {
caseUIImageOrientationDown:
caseUIImageOrientationDownMirrored:
transform =CGAffineTransformTranslate(transform, aImage.size.width, aImage.size.height);
transform =CGAffineTransformRotate(transform,M_PI);
break;
caseUIImageOrientationLeft:
caseUIImageOrientationLeftMirrored:
transform =CGAffineTransformTranslate(transform, aImage.size.width,0);
transform =CGAffineTransformRotate(transform,M_PI_2);
break;
caseUIImageOrientationRight:
caseUIImageOrientationRightMirrored:
transform =CGAffineTransformTranslate(transform,0, aImage.size.height);
transform =CGAffineTransformRotate(transform, -M_PI_2);
break;
default:
break;
}
switch(aImage.imageOrientation) {
caseUIImageOrientationUpMirrored:
caseUIImageOrientationDownMirrored:
transform =CGAffineTransformTranslate(transform, aImage.size.width,0);
transform =CGAffineTransformScale(transform, -1,1);
break;
caseUIImageOrientationLeftMirrored:
caseUIImageOrientationRightMirrored:
transform =CGAffineTransformTranslate(transform, aImage.size.height,0);
transform =CGAffineTransformScale(transform, -1,1);
break;
default:
break;
}
// Now we draw the underlying CGImage into a new context, applying the transform
// calculated above.
CGContextRefctx =CGBitmapContextCreate(NULL, aImage.size.width, aImage.size.height,
CGImageGetBitsPerComponent(aImage.CGImage),0,
CGImageGetColorSpace(aImage.CGImage),
CGImageGetBitmapInfo(aImage.CGImage));
CGContextConcatCTM(ctx, transform);
switch(aImage.imageOrientation) {
caseUIImageOrientationLeft:
caseUIImageOrientationLeftMirrored:
caseUIImageOrientationRight:
caseUIImageOrientationRightMirrored:
CGContextDrawImage(ctx,CGRectMake(0,0,aImage.size.height,aImage.size.width), aImage.CGImage);
break;
default:
CGContextDrawImage(ctx,CGRectMake(0,0,aImage.size.width,aImage.size.height), aImage.CGImage);
break;
}
CGImageRef cgimg =CGBitmapContextCreateImage(ctx);
UIImage *img = [UIImageimageWithCGImage:cgimg];
CGContextRelease(ctx);
CGImageRelease(cgimg);
return img;
}
2.2 拿到上面矫正过的图片,缩小图片尺寸,调用下面方法传入newSize,如(200,200):
+ (UIImage*)imageWithImageSimple:(UIImage*)image scaledToSize:(CGSize)newSize
{
UIGraphicsBeginImageContext(newSize);
[imagedrawInRect:CGRectMake(0,0,newSize.width,newSize.height)];
UIImage* newImage =UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return newImage;
}
2.3 将2.2的图片再压,这个方法可以重复压
//调整大小
NSData *imageData =UIImageJPEGRepresentation(newImage,rate);
NSUIntegersizeOrigin = [image Datalength];//多少KB
NSUIntegersizeOriginKB = sizeOrigin /1024;//多少KB
2.4 上传头像
调用后台接口,把imageData二进制数据上传即可
总结:对图片压缩处理时,在保证图片清晰度变化不大时,减小图片文件大小。方法2.2中的newSize 和 2.3中的rate要以实际效果来设置,我在自己项目中上传的头像最终尺寸是200*200像素,大小为4KB左右。
移动端在图片上传完成后可以让服务端将上传成功的图片的url返回给你
实际开发过程中很有可能会遇到一些比较麻烦的需求:这里就分享下上传图片的时候限制上传的图片的大小的问题,调用系统相册以及调用照相机的实现我就不详细说了,这里主要说一下压缩图片至1M以内的实现,顺带着将file进行base64转码的方法说一下,以下进入主题:
// 选中照片之后的回调方法
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary*)info
{
// 先dismiss调选取照片的VC,在completion block里面完成以下几个操作:
[self dismissViewControllerAnimated:YES completion:^{
UIImage *image = [info objectForKey:UIImagePickerControllerOriginalImage];
// 拍照存入相册
if (picker.sourceType == UIImagePickerControllerSourceTypeCamera) {
UIImageWriteToSavedPhotosAlbum(image, nil, nil, nil);
}
// 判断哪个视图需要照片
UIImage *tempImage = nil;
NSString *imageName = nil;
// 这里注意下:如果有多个上传图片的选项,可以给每个选项设置一个类型,然后每个选项根据相应的类型来设置不同的imageName即可.暂时我只设置一个值,如@"test.jpeg".
imageName = @"test.jpeg";
// 这里:如果有多个上传图片的选项,且给每个选项设置一个类型,这时就可以根据imageName的值的不同,写入本地之后返回的image值也就不同..
// 将返回的本地的图片赋值给tempImage
tempImage = [self writeToCachesWithImage:image imageName:imageName];
// 下面就是调用你自己的上传图片的网络请求了,这是我的网络请求封装的方法....
[self uploadImageWithImage:tempImage imageName:imageName];
}];
}
// 把上传需要的图片转为字符串
- (NSData *)stringWithImage:(UIImage *)image imageName:(NSString *)imageName
{
NSString *imagePath = [[self postImagePathWithFileName:certificationFileName] stringByAppendingPathComponent:imageName];
NSData *imageData = nil;
CGFloat mb = 1.0; // 1MB
CGFloat uploadMB = 1.0; // 1M
CGFloat itemMB = 0; // 把文件转成MB
if ([[NSFileManager defaultManager] fileExistsAtPath:imagePath]) {
long long imageSize = [[[NSFileManager defaultManager] attributesOfItemAtPath:imagePath error:nil] fileSize];
itemMB = imageSize / (mb * 1024 * 1024) ;
} else {
CGImageRef cgImageRef = image.CGImage;
size_t bpp = CGImageGetBitsPerPixel(cgImageRef);
size_t bpc = CGImageGetBitsPerComponent(cgImageRef);
size_t bytes_per_pixel = bpp / bpc;
CGFloat lPixelsPerMB = mb / bytes_per_pixel;
CGFloat totalPixel = CGImageGetWidth(cgImageRef) * CGImageGetHeight(cgImageRef);
itemMB = totalPixel / lPixelsPerMB;
}
if (itemMB = uploadMB) {
imageData = UIImageJPEGRepresentation(image, 1.0f);
} else {
// 大于1M压到1M
CGSize imageSize = image.size;
CGSize toImageSize = CGSizeZero;
if (imageSize.width imageSize.height) {
toImageSize = CGSizeMake(1024, 1024 * imageSize.height / imageSize.width);
} else if (imageSize.width imageSize.height) {
toImageSize = CGSizeMake(1024 * imageSize.width / imageSize.height, 1024);
} else {
toImageSize = CGSizeMake(sqrt(1048576), sqrt(1048576));
}
UIImage *toImage = [image imageCompressForSize:image targetSize:toImageSize];
imageData = UIImageJPEGRepresentation(toImage, 1.f);
}
// 这里注意下:根据你们自己的需要转成不同类型的NSData字节流...
// 这是不用base64转码返回的string
return imageData;
// 这是用base64转码返回的string,1.0上传的方式
// Byte *byte = (Byte *)[imageData bytes];
// return [NSString stringWithFormat:@"jpeg@%@", [GTMBase64 stringByEncodingBytes:byte length:imageData.length]];
}
/* 上传的图片所在的沙盒文件夹
* @param : fileName 文件的名称.
* @param : return 文件的存储路径
*/
- (NSString *)postImagePathWithFileName:(NSString *)fileName
{
NSString *imagePath = [[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject] stringByAppendingPathComponent:fileName];
if (![[NSFileManager defaultManager] fileExistsAtPath:imagePath]) {
[[NSFileManager defaultManager] createDirectoryAtPath:imagePath withIntermediateDirectories:YES attributes:nil error:nil];
}
return imagePath;
}
/*
* 把图片存入本地并返回
* @param : image 选中的image
* @param : imageName 图片的名称
* @return : UIImage
*/
- (UIImage *)writeToCachesWithImage:(UIImage *)image imageName:(NSString *)imageName
{
NSString *imagePath = [[self postImagePathWithFileName:certificationFileName] stringByAppendingPathComponent:imageName];
BOOL isResult = [UIImageJPEGRepresentation(image, 1.0f) writeToFile:imagePath atomically:YES];
if (isResult) {
// 写入成功,把图片取出来
UIImage *certificationImage = [UIImage imageWithContentsOfFile:imagePath];
return certificationImage;
} else {
// 写入失败,直接返回原始图片
return image;
}
}