第七篇、OC_图片的裁剪基于SDWebImage
前期有段时间困扰了我很久一个问题由于工程中的图片数据抓取自不同平台,所以图片的大小尺寸不一定,而放置图片的imageView尺寸是一定的,不作任何处理的话会导致图片拉伸变形,因此找了好久解决办法,现把它拿出来。
#import <UIKit/UIKit.h>
#import "UIImageView+WebCache.h" @interface UIImageView (WebImage) /**
* @author Tucai, 16-02-23 12:02:53
*
* 设置能够自动裁剪的网络图,基于SDWebImage实现
*
*/ // 模糊图渲染
- (void)renderBlurredImageWithUrl:(NSString *)url placeholder:(UIImage *)placeholder completed:(imageDownloadCompletedBlock) completedBlock; //按比例缩放网络图片 - (void)yg_setTrimImageWithUrl:(NSString *)url placeholderImage:(UIImage *)placeholder;
#import "UIImageView+WebImage.h"
#import "NSString+URLEncoding.h"
@implementation UIImageView (WebImage) #pragma mark - 模糊图渲染 - (void)renderBlurredImageWithUrl:(NSString *)url placeholder:(UIImage *)placeholder completed:(imageDownloadCompletedBlock) completedBlock
{
// 这里必须开启内存缓存
[SDWebImageManager sharedManager].imageCache.shouldCacheImagesInMemory = YES; // 渲染背景
__weak typeof(self) ws = self;
[ws sd_setImageWithURL:[NSURL URLWithString:url] completed:^(UIImage *webImage, NSError *error, SDImageCacheType cacheType, NSURL *imageURL) {
// 999 是一个标记
if (ws.tag != ) {
UIVisualEffectView *visualView = [[UIVisualEffectView alloc] initWithFrame:ws.bounds];
UIBlurEffect *effect = [UIBlurEffect effectWithStyle:UIBlurEffectStyleLight];
visualView.effect = effect;
NSLog(@"only once");
[ws addSubview:visualView];
ws.tag = ;
}
ws.alpha =0.6;
ws.image = nil;
ws.image = webImage;
if (completedBlock) {
completedBlock(webImage);
}
}];
} #pragma mark - 裁剪图片 - (void)yg_setTrimImageWithUrl:(NSString *)url placeholderImage:(UIImage *)placeholder{ __weak typeof(self) ws = self;
[SDWebImageManager sharedManager].imageCache.shouldCacheImagesInMemory = NO; [self sd_setImageWithURL:[NSURL URLWithString:url] placeholderImage:placeholder completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, NSURL *imageURL) {
if (image) {
UIImage *img=[self yg_trimImageWithImage:image];
ws.image=img;
}else{
ws.image =[self yg_trimImageWithImage:placeholder];
}
}];
} -(UIImage *)yg_trimImageWithImage:(UIImage *)image{ //imageView的宽高比
CGFloat imageViewWidthHeightRatio =self.frame.size.width/self.frame.size.height;
//屏幕分辨率
// CGFloat imageScale = [[UIScreen mainScreen] scale]; CGFloat imageScale = ; CGFloat imageWith = image.size.width*imageScale; CGFloat imageHeight =image.size.height*imageScale; //image的宽高比
CGFloat imageWidthHeightRatio =imageWith/imageHeight; CGImageRef imageRef = nil; CGRect rect; // NSLog(@"\nimageWith === %f\nimageHeight === %f\nImageView宽高比 == %f\nimageScale == %f",imageWith,imageHeight,imageViewWidthHeightRatio,imageScale); if (imageWidthHeightRatio>imageViewWidthHeightRatio) { rect = CGRectMake((imageWith-imageHeight*imageViewWidthHeightRatio)/, , imageHeight*imageViewWidthHeightRatio, imageHeight); }else if (imageWidthHeightRatio<imageViewWidthHeightRatio) { rect = CGRectMake(, (imageHeight-imageWith/imageViewWidthHeightRatio)/, imageWith, imageWith/imageViewWidthHeightRatio); }else {
rect = CGRectMake(, , imageWith, imageHeight);
} imageRef = CGImageCreateWithImageInRect([image CGImage], rect);
UIImage *res = [UIImage imageWithCGImage:imageRef scale:imageScale orientation:UIImageOrientationUp]; /**
一定要,千万要release,否则等着内存泄露吧,稍微高清点的图一张图就是几M内存,很快App就挂了
*/
CGImageRelease(imageRef); return res;
} @end
第七篇、OC_图片的裁剪基于SDWebImage的更多相关文章
- 图片裁剪(基于RxPaparazzo)
图片裁剪(基于RxPaparazzo) 前言:基于RxPaparazzo的图片裁剪,图片旋转.比例放大|缩小. 效果: 开发环境:AndroidStudio2.2.1+gradle-2.14.1 涉及 ...
- C#设计模式总结 C#设计模式(22)——访问者模式(Vistor Pattern) C#设计模式总结 .NET Core launch.json 简介 利用Bootstrap Paginator插件和knockout.js完成分页功能 图片在线裁剪和图片上传总结 循序渐进学.Net Core Web Api开发系列【2】:利用Swagger调试WebApi
C#设计模式总结 一. 设计原则 使用设计模式的根本原因是适应变化,提高代码复用率,使软件更具有可维护性和可扩展性.并且,在进行设计的时候,也需要遵循以下几个原则:单一职责原则.开放封闭原则.里氏代替 ...
- 用仿ActionScript的语法来编写html5——第七篇,自定义按钮
第七篇,自定义按钮这次弄个简单点的,自定义按钮.其实,有了前面所定义的LSprite,LBitmap等类,定义按钮就很方便了.下面是添加按钮的代码, function gameInit(event){ ...
- Python开发【第七篇】:面向对象 和 python面向对象进阶篇(下)
Python开发[第七篇]:面向对象 详见:<Python之路[第五篇]:面向对象及相关> python 面向对象(进阶篇) 上一篇<Python 面向对象(初级篇)> ...
- ElasticSearch入门 第七篇:分词
这是ElasticSearch 2.4 版本系列的第七篇: ElasticSearch入门 第一篇:Windows下安装ElasticSearch ElasticSearch入门 第二篇:集群配置 E ...
- WCF开山篇__图片传输
WCF开山篇__图片传输 一. 简介 Windows Communication Foundation(WCF)是由微软发展的一组数据通信的应用程序接口,可以翻译为Windows通讯接口,它是. ...
- 详细解释如何通过Android自带的方式来实现图片的裁剪——原理分析+解决方案
我们很多时候需要进行图片的裁剪,其实这个功能在android系统中已经有一套解决方案了,虽然界面和效果并不是很优秀但功能毫无疑问是完美实现了.至于,不用自带的方案怎么做自定义,这个就是后话了.本篇主要 ...
- 通过Nginx訪问FastDFS文件系统并进行图片文件裁剪的性能測试和分析
前段时间公司的分布式图片文件系统(FastDFS)做了图片裁剪和缩放功能,并把缩放计算和FastDFS做了解耦分离,前端用虚拟机作为图片文件缩放的訪问代理层(Nginx Proxy),后端使用ngin ...
- 跟我学SpringCloud | 第十七篇:服务网关Zuul基于Apollo动态路由
目录 SpringCloud系列教程 | 第十七篇:服务网关Zuul基于Apollo动态路由 Apollo概述 Apollo相比于Spring Cloud Config优势 工程实战 示例代码 Spr ...
随机推荐
- Codeforces Round #336 (Div. 2) C. Chain Reaction set维护dp
C. Chain Reaction 题目连接: http://www.codeforces.com/contest/608/problem/C Description There are n beac ...
- CSS Sprites图片处理
简介: CSS Sprites是一个网页图片处理方式,在国内都叫CSS精灵,css Sprites允许你将一个页面涉及到的所有零星图片都包含到一张大图中去,这样一来,当访问该页面时,载入的图片就不会像 ...
- Android 深入ViewPager补间动画,实现类京东商城首页广告Banner切换效果
如有转载,请声明出处: 时之沙: http://blog.csdn.net/t12x3456 某天看到京东商城首页的滑动广告的Banner,在流动切换的时候有立体的动画效果,感觉很有意思,然后研究了下 ...
- Visual C++内存泄露检测—VLD工具使用说明[转]
Visual C++内存泄露检测—VLD工具使用说明 一. VLD工具概述 Visual Leak Detector(VLD)是一款用于Visual C++的免费的内存泄露检测工具.他的 ...
- TP第2个项目总结
1.参数尼玛传接受要非数字方法 $id = $this->_get('id','intval',0); $field_wc = $this->_get('wc_field'); 2.参数不 ...
- 翻译学python---《Learn Python the hard Way》---第一章 绪论
打算学习python,但是又不想单纯地看书或是写个小项目,干脆引入很流行的翻译学习法来学习吧- 在论坛上看到了国外的一本<Learn Python the hard Way> ...
- jq实现地址级联效果
(function ($) { $.fn.Address = function (options) { var defaults = { divid: "Address", cal ...
- LeetCode31 Next Permutation
题目: Implement next permutation, which rearranges numbers into the lexicographically next greater per ...
- .net对各表的操作详细到字段的更改记录的日志
存入数据库中,目前的字段包括操作人,操作时间,sql语句,被修改的字段,字段原值,操作人的身份. /// <summary> /// 添加操作日志 /// </summary> ...
- 【Shell脚本学习13】Shell数组:shell数组的定义、数组长度
Shell在编程方面比Windows批处理强大很多,无论是在循环.运算. bash支持一维数组(不支持多维数组),并且没有限定数组的大小.类似与C语言,数组元素的下标由0开始编号.获取数组中的元素要利 ...