美文网首页
OC中关于图片的处理 压和缩处理

OC中关于图片的处理 压和缩处理

作者: Augus_man | 来源:发表于2017-10-26 17:36 被阅读17次

最近公司处理图片 前端上传的时候 总是会出现图片上传失败  我们用的是base64和后台交互的,但是base64有大小限制 大约在750--800k之间,超过这个就会无法跟后台进行正常的请求交互.而我们的客户群体又是比较广泛,谁也不知道上帝会给你传多大的照片,而我作为开发,背锅那是肯定的了.为了解决上帝的烦恼呢,就不停地筛选查看文献,最终在俩位大神的文章帮助下自己综合了一下(http://www.jb51.net/article/89491.htm和http://blog.csdn.net/u012603758/article/details/52787225).

代码如下(自己写了个分类扩展)

.h

#import<UIKit/UIKit.h>

@interface UIImage (YZJImageScale)

//image: 处理的图片  

//kb:处理后的大小 单位kb

//size:处理后的尺寸(压之后 还大于kb的话才会缩)

+(NSData*)scaleImage:(UIImage *)image toKb:(NSInteger)kb withSize:(CGSize )size;

@end

.m

#import "UIImage+YZJImageScale.h"

@implementation UIImage (YZJImageScale)

+(NSData *)scaleImage:(UIImage *)image toKb:(NSInteger)kb withSize:(CGSize )size{

//压

if (kb<1) {

return nil;

}

kb*=1024;

CGFloat compression = 0.4f;

CGFloat maxCompression = 0.1f;

NSData *imageData = UIImageJPEGRepresentation(image, compression);

while ([imageData length] > kb && compression > maxCompression) {

compression -= 0.1;

imageData = UIImageJPEGRepresentation(image, compression);

}

//缩

if ([imageData length]>kb&&compression<0.1) {

UIImage *newImage=[UIImage imageWithData:imageData];

UIImage *targetImg=[[self new] imageByScalingAndCroppingForSize:size withSourceImage:newImage];

imageData= UIImageJPEGRepresentation(targetImg, 0.7f);

}

return imageData;

}

- (UIImage*)imageByScalingAndCroppingForSize:(CGSize)targetSize withSourceImage:(UIImage *)sourceImage

{

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;

}

@end

相关文章

  • OC中关于图片的处理 压和缩处理

    最近公司处理图片 前端上传的时候 总是会出现图片上传失败 我们用的是base64和后台交互的,但是base64有大...

  • iOS Tips

    开发中 可能会用到的一些 方法 关于图片处理(UIImage) 将颜色转化为图片 修改图片大小_ OC 修改图片大...

  • Swift中的图片处理库Kingfisher

    在oc时代有个非常强大图片缓存处理的库SDWebImage,swift中现在也有个不错的图片处理的库----Kin...

  • iOS图片压缩----中的‘压’和‘缩’

    1、图片的“压”和“缩”概念 “压” 是指文件体积变小,但是像素数不变,长宽尺寸不变,那么质量可能下降。“缩” 是...

  • 超实用的图片压缩神器推荐,再也不用求人啦!

    上期给大家推荐的图片去水印软件:《超厉害的图片去水印黑科技,用起来!》,今天又来一批关于处理图片使用的工具:图片压...

  • 关于聊天图片的压与缩

    今天来公司加班,准备搞一下聊天时发送拍照图片慢的问题,因为之前也做了一些对图片的处理,但是发送的过程中存在延迟。之...

  • iOS 压缩图片

    前言: 压缩图片其实不是一个动作,而是两个:“压”和“缩” 压:损失图片的质量,直观的感觉就是清晰度下降缩:缩小图...

  • GD库使用

    定义:GD库提供了一系列用来处理图片的API ,使用GD库可以处理图片,或者生成图片。在网站上GD库通常用来生成缩...

  • iOS 图片置灰(OC&Swift)

    对于项目中需要图片置灰的处理方法: OC: //将图片置灰+ (UIImage*)grayScale:(UIIma...

  • 不会PS没关系,6个网站助你高效完成图片处理

    在学习和工作中,经常需要对图片进行处理,但专业的图片处理软件又太难学,所以今天给大家推荐6个图片处理网站,方便大家...

网友评论

      本文标题:OC中关于图片的处理 压和缩处理

      本文链接:https://www.haomeiwen.com/subject/vfikpxtx.html