自己写的一个网络请求图片居中工具类
+(void)cropToolWithUrl:(NSString *)url imageView:(UIImageView *)imageView{
[imageView sd_setImageWithURL:[NSURL URLWithString:url] completed:^(UIImage * _Nullable image, NSError * _Nullable error, SDImageCacheType cacheType, NSURL * _Nullable imageURL) {
CGFloat width = image.size.width;
CGFloat height = image.size.height;
if (width==height) {
}else{
CGSize contextSize;
CGFloat context_x_ctm;
if(width>height){
contextSize=CGSizeMake(height, height);
context_x_ctm=-(width-height)/2;
}else{
contextSize=CGSizeMake(width, width);
context_x_ctm=-(height-width)/2;
}
UIImage *croppedImage = nil;
UIGraphicsBeginImageContextWithOptions(contextSize, true,1.0);
{
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextTranslateCTM(context, context_x_ctm, 0);
[image drawAtPoint:CGPointZero];
croppedImage = UIGraphicsGetImageFromCurrentImageContext();
}
UIGraphicsEndImageContext();
UIImage *newImg = [UIImage imageWithCGImage:croppedImage.CGImage scale:[UIScreen mainScreen].scale orientation:UIImageOrientationUp];
imageView.image=newImg;
}
}];
}
网友评论