Commit 652f0651eb4611f139a9e8d971c126d36ad5abaf

Authored by zhangxiaowen
1 parent b14c0f68

no message

CNLivePreloadResources/Classes/CNLivePreloadManager.h
@@ -16,13 +16,14 @@ NS_ASSUME_NONNULL_BEGIN @@ -16,13 +16,14 @@ NS_ASSUME_NONNULL_BEGIN
16 * 预加载资源 16 * 预加载资源
17 * 17 *
18 * @param preloadId 加载标识 18 * @param preloadId 加载标识
19 - * @param type 资源类型 19 + * @param type 资源类型(目前只支持图片类型CNLiveResourceImage)
20 * @param imageNames 资源名字数组 20 * @param imageNames 资源名字数组
21 * 21 *
22 * @return 模型 22 * @return 模型
23 */ 23 */
24 + (CNLivePreloadModel *)preloadModelForPreloadId:(NSString *)preloadId type:(CNLiveResourceType)type imageNames:(NSArray<NSString *> *)imageNames; 24 + (CNLivePreloadModel *)preloadModelForPreloadId:(NSString *)preloadId type:(CNLiveResourceType)type imageNames:(NSArray<NSString *> *)imageNames;
25 25
  26 +
26 #pragma mark - 针对具体业务 27 #pragma mark - 针对具体业务
27 /** 28 /**
28 * 预加载全部资源 29 * 预加载全部资源
CNLivePreloadResources/Classes/CNLivePreloadManager.m
@@ -206,36 +206,40 @@ static NSMutableDictionary *resources; @@ -206,36 +206,40 @@ static NSMutableDictionary *resources;
206 + (void)asyncLoadResources:(CNLivePreloadModel *)model type:(CNLiveResourceType)type{ 206 + (void)asyncLoadResources:(CNLivePreloadModel *)model type:(CNLiveResourceType)type{
207 model.isLoading = YES; 207 model.isLoading = YES;
208 dispatch_async(dispatch_get_global_queue(0, 0), ^{ 208 dispatch_async(dispatch_get_global_queue(0, 0), ^{
209 - NSMutableArray<UIImage *> *images = [[NSMutableArray alloc] init];  
210 - for (CNLiveResourceModel *resourceModel in model.models) {  
211 - if (resourceModel.image == nil) {  
212 - UIImage *image = nil;  
213 - switch (type) {  
214 - case CNLiveResourceImage:  
215 - {  
216 - image = [CNLivePreloadManager cn_getImageName:resourceModel.name bundle:@"CNLivePreloadResources.bundle/CNLivePreloadResources" targetClass:[CNLivePreloadManager class]]; 209 + switch (type) {
  210 + case CNLiveResourceImage:
  211 + {
  212 + NSMutableArray<UIImage *> *images = [[NSMutableArray alloc] init];
  213 + for (CNLiveResourceModel *resourceModel in model.models) {
  214 + if (resourceModel.image == nil) {
  215 + UIImage *image = [CNLivePreloadManager cn_getImageName:resourceModel.name bundle:@"CNLivePreloadResources.bundle/CNLivePreloadResources" targetClass:[CNLivePreloadManager class]];;
  216 + if (image == nil) {
  217 + model.isLoadFailed = YES;
  218 + }
  219 + else{
  220 + resourceModel.image = image;
  221 + [images addObject:image];
  222 + model.isLoadFailed = NO;
  223 + }
217 } 224 }
218 - break;  
219 -  
220 - case CNLiveResourceGif:  
221 - {  
222 - image = [CNLivePreloadManager cn_getImageName:resourceModel.name bundle:@"CNLivePreloadResources.bundle/CNLivePreloadResources" targetClass:[CNLivePreloadManager class]];  
223 - }  
224 - break;  
225 -  
226 - }  
227 - if (image == nil) {  
228 - model.isLoadFailed = YES;  
229 - }  
230 - else{  
231 - resourceModel.image = image;  
232 - [images addObject:image];  
233 - model.isLoadFailed = NO;  
234 } 225 }
  226 + model.images = [NSArray arrayWithArray:images];
  227 + model.isLoading = NO;
235 } 228 }
  229 + break;
  230 + case CNLiveResourceGif:
  231 + {
  232 +
  233 + }
  234 + break;
  235 + case CNLiveResourceText:
  236 + {
  237 +
  238 + }
  239 + break;
  240 +
236 } 241 }
237 - model.images = [NSArray arrayWithArray:images];  
238 - model.isLoading = NO; 242 +
239 }); 243 });
240 244
241 } 245 }
CNLivePreloadResources/Classes/CNLivePreloadModel.h
@@ -18,13 +18,14 @@ typedef NS_ENUM(NSInteger, CNLivePreloadType) { @@ -18,13 +18,14 @@ typedef NS_ENUM(NSInteger, CNLivePreloadType) {
18 18
19 }; 19 };
20 20
21 -//资源类型 21 +//资源类型(目前只支持图片类型)
22 typedef NS_ENUM(NSInteger, CNLiveResourceType) { 22 typedef NS_ENUM(NSInteger, CNLiveResourceType) {
23 CNLiveResourceImage, // 动画图片 23 CNLiveResourceImage, // 动画图片
24 - CNLiveResourceGif // Gif  
25 - 24 + CNLiveResourceGif, // Gif
  25 + CNLiveResourceText // 文本
26 }; 26 };
27 27
  28 +// 资源类型(1.图片 2.Gif 3.文本)
28 @interface CNLiveResourceModel : NSObject 29 @interface CNLiveResourceModel : NSObject
29 // 标识 30 // 标识
30 @property (nonatomic, copy) NSString *ID; 31 @property (nonatomic, copy) NSString *ID;
@@ -41,6 +42,7 @@ typedef NS_ENUM(NSInteger, CNLiveResourceType) { @@ -41,6 +42,7 @@ typedef NS_ENUM(NSInteger, CNLiveResourceType) {
41 42
42 @end 43 @end
43 44
  45 +// 某一种类型的资源(1.刷新图片 2.Gif封面 3.Emotion)
44 @interface CNLivePreloadModel : NSObject 46 @interface CNLivePreloadModel : NSObject
45 // 是否存在加载失败 47 // 是否存在加载失败
46 @property (nonatomic, assign) BOOL isLoadFailed; 48 @property (nonatomic, assign) BOOL isLoadFailed;
@@ -50,8 +52,8 @@ typedef NS_ENUM(NSInteger, CNLiveResourceType) { @@ -50,8 +52,8 @@ typedef NS_ENUM(NSInteger, CNLiveResourceType) {
50 @property (nonatomic, copy) NSString *ID; 52 @property (nonatomic, copy) NSString *ID;
51 // UIImage 53 // UIImage
52 @property (nonatomic, strong) NSArray *images; 54 @property (nonatomic, strong) NSArray *images;
  55 +// CNLiveResourceModel数组
53 @property (nonatomic, strong) NSArray<CNLiveResourceModel *> *models; 56 @property (nonatomic, strong) NSArray<CNLiveResourceModel *> *models;
54 -  
55 // 类型 57 // 类型
56 @property (nonatomic, assign) CNLivePreloadType type; 58 @property (nonatomic, assign) CNLivePreloadType type;
57 59
Example/CNLivePreloadResources/CNLiveViewController.m
@@ -37,6 +37,7 @@ @@ -37,6 +37,7 @@
37 37
38 NSArray *add2 = [CNLivePreloadManager preloadImagesForEmotion]; 38 NSArray *add2 = [CNLivePreloadManager preloadImagesForEmotion];
39 image2.image = add2[2]; 39 image2.image = add2[2];
  40 +
40 }); 41 });
41 } 42 }
42 43