只是个人的一些愚见
在我们写一个table表的时候会遇到这样的问题,就是设计给到的设计方案是一个有着单张图片显示到指定大小的,然后后台只给我们一张url 地址的尴尬局面,如果我们拿到图片后再去获得图片大小再反向刷高度会感觉蹦来蹦去的
不是那么友好,,,,
在这种条件下本人想改如何才能找到一种方案是先获取到大小而不下载图片呢,看了一些文章觉得这个方法是可行的就是在获取的时候是在主线程获取的
#暂时的解决方案
将下来的数据如果是单张图片先缓存大小在去下载图片
下面是主线程获取图片信息的方法 注意在主线程 中!!!
#import "UIImage+ImgSize.h"
#import <ImageIO/ImageIO.h>
@implementation UIImage (ImgSize)
/**
* 根据图片url获取网络图片尺寸
*/
+ (CGSize)getImageSizeWithURL:(id)URL{
NSURL * url = nil;
if ([URL isKindOfClass:[NSURL class]]) {
url = URL;
}
if ([URL isKindOfClass:[NSString class]]) {
url = [NSURL URLWithString:URL];
}
if (!URL) {
return CGSizeZero;
}
CGImageSourceRef imageSourceRef = CGImageSourceCreateWithURL((CFURLRef)url, NULL);
CGFloat width = 0, height = 0;
if (imageSourceRef) {
// 获取图像属性
CFDictionaryRef imageProperties = CGImageSourceCopyPropertiesAtIndex(imageSourceRef, 0, NULL);
//以下是对手机32位、64位的处理
if (imageProperties != NULL) {
CFNumberRef widthNumberRef = CFDictionaryGetValue(imageProperties, kCGImagePropertyPixelWidth);
#if defined(__LP64__) && __LP64__
if (widthNumberRef != NULL) {
CFNumberGetValue(widthNumberRef, kCFNumberFloat64Type, &width);
}
CFNumberRef heightNumberRef = CFDictionaryGetValue(imageProperties, kCGImagePropertyPixelHeight);
if (heightNumberRef != NULL) {
CFNumberGetValue(heightNumberRef, kCFNumberFloat64Type, &height);
}
#else
if (widthNumberRef != NULL) {
CFNumberGetValue(widthNumberRef, kCFNumberFloat32Type, &width);
}
CFNumberRef heightNumberRef = CFDictionaryGetValue(imageProperties, kCGImagePropertyPixelHeight);
if (heightNumberRef != NULL) {
CFNumberGetValue(heightNumberRef, kCFNumberFloat32Type, &height);
}
#endif
/********************** 此处解决返回图片宽高相反问题 **********************/
// 图像旋转的方向属性
NSInteger orientation = [(__bridge NSNumber *)CFDictionaryGetValue(imageProperties, kCGImagePropertyOrientation) integerValue];
CGFloat temp = 0;
switch (orientation) {
case UIImageOrientationLeft: // 向左逆时针旋转90度
case UIImageOrientationRight: // 向右顺时针旋转90度
case UIImageOrientationLeftMirrored: // 在水平翻转之后向左逆时针旋转90度
case UIImageOrientationRightMirrored: { // 在水平翻转之后向右顺时针旋转90度
temp = width;
width = height;
height = temp;
}
break;
default:
break;
}
/********************** 此处解决返回图片宽高相反问题 **********************/
CFRelease(imageProperties);
}
CFRelease(imageSourceRef);
}
return CGSizeMake(width, height);
}
@end
获取到以后 将每一个图片地址作为key 存储到当前运行内存中,然后下次再有该图片地址的时候先查询有没有缓存再进行操作,但是我个人觉得还是有点卡线程 有没有好一点的解决方案呢! 最好还是后端给数据
能不能在有一个单独的控制器去管控这类型的数据呢??

网友评论