YSImage.h 数据模型模型
@interface YSImage : NSObject
@property (nonatomic, strong) NSURL *imageURL;
@property (nonatomic, assign) CGFloat imageW;
@property (nonatomic, assign) CGFloat imageH;
+ (instancetype)imageWithImageDic:(NSDictionary *)imageDic;
@end
YSImage.m 实现方法
@implementation YSImage
+ (instancetype)imageWithImageDic:(NSDictionary *)imageDic {
YSImage *image = [[YSImage alloc] init];
image.imageURL = [NSURL URLWithString:imageDic[@"img"]];
image.imageW = [imageDic[@"w"] floatValue];
image.imageH = [imageDic[@"h"] floatValue];
return image;
}
@end
使用的地方
- (NSMutableArray *)images {
//从plist文件中取出字典数组,并封装成对象模型,存入模型数组中
if (!_images) {
_images = [NSMutableArray array];
NSString *path = [[NSBundle mainBundle] pathForResource:@"1.plist" ofType:nil];
NSArray *imageDics = [NSArray arrayWithContentsOfFile:path];
for (NSDictionary *imageDic in imageDics) {
YSImage *image = [YSImage imageWithImageDic:imageDic];
[_images addObject:image];
}
}
return _images;
}