处理下载后的图片不是正方形的问题
在 上面 调用SDWebImage 去请求图片
[_hotelImage sd_setImageWithURL:[NSURL URLWithString:model.hotelLogo] completed:^(UIImage *image,NSError*error,SDImageCacheTypecacheType,NSURL*imageURL) {
if(!image)
【处理下载后的图片不是正方形的问题】{
image = [UIImage imageNamed:@"placeholder_hotel_list"];
}
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH,0), ^{
UIImage*overlayImage = [self squareImageFromImage:image scaledToSize:kScreenWidth/2];
dispatch_async(dispatch_get_main_queue(), ^{
_hotelImage.image= overlayImage;
});
});
}];
/**
*剪切图片为正方形
*
*@paramimage原始图片比如size大小为(400x200)pixels
*@paramnewSize 正方形的size比如400pixels
*
*@return返回正方形图片(400x400)pixels
*/
- (UIImage*)squareImageFromImage:(UIImage*)image scaledToSize:(CGFloat)newSize {
CGAffineTransform scaleTransform;
CGPoint origin;
if (image.size.width> image.size.height) {
//image原始高度为200,缩放image的高度为400pixels,所以缩放比率为2
CGFloat scaleRatio = newSize / image.size.height;
scaleTransform =CGAffineTransformMakeScale(scaleRatio, scaleRatio);
//设置绘制原始图片的画笔坐标为CGPoint(-100, 0)pixels
origin =CGPointMake(-(image.size.width- image.size.height) /2.0f,0);
} else {
CGFloat scaleRatio = newSize / image.size.width;
scaleTransform =CGAffineTransformMakeScale(scaleRatio, scaleRatio);
origin =CGPointMake(0, -(image.size.height- image.size.width) /2.0f);
}
CGSizesize =CGSizeMake(newSize, newSize);
//创建画板为(400x400)pixels
if ([[UIScreen mainScreen]respondsToSelector:@selector(scale)]) {
UIGraphicsBeginImageContextWithOptions(size,YES,0);
} else {
UIGraphicsBeginImageContext(size);
}
CGContextRef context =UIGraphicsGetCurrentContext();
//将image原始图片(400x200)pixels缩放为(800x400)pixels
CGContextConcatCTM(context, scaleTransform);
//origin也会从原始(-100, 0)缩放到(-200, 0)
[image drawAtPoint:origin];
//获取缩放后剪切的image图片
image =UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return image;
}
推荐阅读
- 小程序web-view嵌入公众号网页并实现微信支付、下载pdf
- SSM|SpringMVC拦截器 & SpringMVC异常处理
- java中自定义拦截器_JavaEE开发之SpringMVC中的自定义拦截器及异常处理
- 科学计算软件哪个好(Wolfram Mathematica 13中文版下载)
- 图像处理|图像处理之阈值分割[全局阈值、Otsu阈值和迭代式阈值分割]
- 图像处理|图像处理之图像复原[逆滤波、维纳滤波、约束最小二乘法、Lucy-Richardson和盲解卷积复原]
- 图像处理常用边缘检测算子
- 南极来信
- 新一代分布式实时流处理引擎Flink入门实战操作篇
- [PHP错误异常]①⑥--自定义异常处理器