您可以在获取它之后调整图像大小。只是通过图像和图像的高度和宽度。
[cell.imgview sd_setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@",Image_URL2]] placeholderImage:[UIImage imageNamed:@"imagename"] completed:^(UIImage image, NSError error, SDImageCacheType cacheType, NSURL *imageURL) {
if (image) {
cell.imgview.image=[Utility compressImageDynamic:image hight:(360 [UIScreen mainScreen].bounds.size.width)/320 width:(360 [UIScreen mainScreen].bounds.size.width)/320];
}
else{
}
}];
以下是reSize图像的函数。
-(UIImage)compressImageDynamic:(UIImage)image hight:(float)x width:(float)y{
float actualHeight = image.size.height;
float actualWidth = image.size.width;
float maxHeight = x;
float maxWidth = y;
float imgRatio = actualWidth/actualHeight;
float maxRatio = maxWidth/maxHeight;
float compressionQuality = 1;//50 percent compression
if (actualHeight > maxHeight || actualWidth > maxWidth){
if(imgRatio < maxRatio){
//adjust width according to maxHeight
imgRatio = maxHeight/actualHeight;
actualWidth = imgRatio * actualWidth;
actualHeight = maxHeight;
}
else if(imgRatio > maxRatio){
//adjust height according to maxWidth
imgRatio = maxWidth/actualWidth;
actualHeight = imgRatio * actualHeight;
actualWidth = maxWidth;
}
else{
actualHeight = maxHeight;
actualWidth = maxWidth;
}
}else{
actualHeight = maxHeight;
actualWidth = maxWidth;
compressionQuality = 1;
}
CGRect rect = CGRectMake(0.0, 0.0, actualWidth, actualHeight);
UIGraphicsBeginImageContext(rect.size);
[image drawInRect:rect];
UIImage *img = UIGraphicsGetImageFromCurrentImageContext();
NSData *imageData = UIImageJPEGRepresentation(img, compressionQuality);
UIGraphicsEndImageContext();
return [UIImage imageWithData:imageData];
}
这就是我的想法,但我不知道如何实现它与SDWebImage ..我会尝试你的版本,似乎是正确的,非常感谢! –
对不起,对于迟到的答案,但我终于尝试了它,它似乎完美:)非常感谢! –