您的位置:首頁>科技>正文

5種將死的程式設計語言

Assets.xcassets

Data Set

一直很推薦使用Assets.xcassets來管理圖片, 就一個字“方便”。 用的很順手啊。 但是Gif的圖片一直都是放在Resouces資料夾中的, 今天突然想著為何不把Gif的圖片也放入Asset.xcassets中進行管理。 可怎麼使用呢。

New Data Set

列表中唯一看到能使用的, 應該只有New Data Set了。 但是這個Data Set Type怎麼使用呢。 查找了官方文檔和網路上的文章, 都沒有一個具體的說明。

添加Gif

右鍵選擇New Data Set後, 出現一個Universal的圖片框, 將Gif檔拖入。

Gif

Gif的圖片就放入到了Assets.xcassets中。 Contents.json顯示為:

12345678910111213{ "info" : { "version" : 1, "author" : "xcode" }, "data" : [ { "idiom" : "universal", "filename" : "timg.gif", "universal-type-identifier" : "com.compuserve.gif" } ]}

載入Gif

因為不是png圖片, 所以不能使用[UIImage imageNamed:@"bear"];來獲取, 執行這個返回nil。 而需要通過根據路徑來載入Assets.xcassets中圖片。

123456NSString *gifPath = [[NSBundle mainBundle] pathForResource:@"bear" ofType:@"gif"]; NSData *gif = [NSData dataWithContentsOfFile:gifPath]; FLAnimatedImage *image = [FLAnimatedImage animatedImageWithGIFData:gif]; [self.gifImageView setAnimatedImage:image];

運行代碼, 執行成功。

播放

FLAnimatedImage

FLAnimatedImage支援Gif的顯示。 如果直接使用UIImageView來顯示的動畫, 也是可以的, 不過需要載入很多圖片。

Gif拆解過多張圖片

使用UIImageView的@property (nullable, nonatomic, copy) NSArray*animationImages;來實現動效。

123456789101112NSInteger pages = 45; NSMutableArray *imagesMArr = [[NSMutableArray alloc] initWithCapacity:5]; for (int i = 1; i <= pages; i++ ) { NSString *imageName = [NSString stringWithFormat:@"loading_%d", i]; UIImage *image = [UIImage imageNamed:imageName]; [imagesMArr addObject:image]; } [self.gifImageView setAnimationImages:imagesMArr]; [self.gifImageView startAnimating];

效果如下:

AnimationImages的動效

但是如你所見, 為了實現這個效果, 保存了整整45張圖片。 最後會導致APP的包變大, 之後更換圖片也比較麻煩。 還是果斷使用FLAnimatedImage吧。 雖然這個包已經很久沒更新了。

// END

開發任務一直不需要用到新的技術, 感覺用之前的經驗就可以混日子了。 !_!

Next Article
喜欢就按个赞吧!!!
点击关闭提示