建议使用方法二
方法一:
//通过图片的URL地址,从网络上获取图片(UIImage类型)
-(UIImage *)getImageFromURL:(NSString *)fileURL{
UIImage * result;
NSData * data = [NSData dataWithContentsOfURL:[NSURL URLWithString:fileURL]];
result = [UIImage imageWithData:data];
//tabbar上的图片需要的尺寸 44x44
CGSize size22 = CGSizeMake(25, 25);
UIGraphicsBeginImageContext(size22);
// CGSize size22 = [UIImage getImageSizeWithURL:[NSURL URLWithString:fileURL]];
// UIGraphicsBeginImageContext(size22);
[result drawInRect:CGRectMake(0, 0, size22.width, size22.height)];
UIImage *newImage2 = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
result = newImage2;
return result;
}
方法二:
//导入头文件
#import "SDImageCache.h"
// 加载图片
- (void)loadImage:(NSString *)image complet:(void(^)(UIImage *img))complet {
// 设置图片 获取本地缓存的图片
UIImage *newImage = [[SDImageCache sharedImageCache] imageFromDiskCacheForKey:image];
if (newImage != nil) {
// newImage就是图片 压缩尺寸25*25
UIImage *newImage1 = [self imageCompressForSize:newImage targetSize:CGSizeMake(25, 25)];
complet(newImage1);
} else {
//下载图片
[[SDWebImageManager sharedManager] loadImageWithURL:[NSURL URLWithString:image] options:0 progress:^(NSInteger receivedSize, NSInteger expectedSize, NSURL * _Nullable targetURL) {
} completed:^(UIImage * _Nullable image, NSData * _Nullable data, NSError * _Nullable error, SDImageCacheType cacheType, BOOL finished, NSURL * _Nullable imageURL) {
if (image) {//下载完成后
//同上处理
UIImage *newImage = [self imageCompressForSize:image targetSize:CGSizeMake(25, 25)];
complet(newImage);
}
}];
// [[SDWebImageManager sharedManager] downloadImageWithURL:[NSURL URLWithString:image] options:0 progress:^(NSInteger receivedSize, NSInteger expectedSize) {
//
// } completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, BOOL finished, NSURL *imageURL) {
// if (image) {//下载完成后
// //同上处理
// UIImage *newImage = [self imageCompressForSize:image targetSize:CGSizeMake(25, 25)];
// complet(newImage);
// }
// }];
}
}
//图片压缩色值不变
- (UIImage *)imageCompressForSize:(UIImage *)sourceImage targetSize:(CGSize)size{
UIImage *newImage = nil;
CGSize imageSize = sourceImage.size;
CGFloat width = imageSize.width;
CGFloat height = imageSize.height;
CGFloat targetWidth = size.width;
CGFloat targetHeight = size.height;
CGFloat scaleFactor = 0.0;
CGFloat scaledWidth = targetWidth;
CGFloat scaledHeight = targetHeight;
CGPoint thumbnailPoint = CGPointMake(0.0, 0.0);
if(CGSizeEqualToSize(imageSize, size) == NO){
CGFloat widthFactor = targetWidth / width;
CGFloat heightFactor = targetHeight / height;
if(widthFactor > heightFactor){
scaleFactor = widthFactor;
} else{
scaleFactor = heightFactor;
}
scaledWidth = width * scaleFactor;
scaledHeight = height * scaleFactor;
if(widthFactor > heightFactor){
thumbnailPoint.y = (targetHeight - scaledHeight) * 0.5;
}else if(widthFactor < heightFactor){
thumbnailPoint.x = (targetWidth - scaledWidth) * 0.5;
}
}
UIGraphicsBeginImageContextWithOptions(size, NO, 3.0);
CGRect thumbnailRect = CGRectZero;
thumbnailRect.origin = thumbnailPoint;
thumbnailRect.size.width = scaledWidth;
thumbnailRect.size.height = scaledHeight;
[sourceImage drawInRect:thumbnailRect];
newImage = UIGraphicsGetImageFromCurrentImageContext();
if(newImage == nil){
NSLog(@"scale image fail");
}
UIGraphicsEndImageContext();
return newImage;
}
网友评论