2013-07-25 75 views
5

我使用了「mayoff」(Rob Mayoff)「UIImageView + animatedGIF」製作的類,這是在stackoverflow上的答案之一中提出的。 UIImageView+animatedGIFUIImageView +動畫GIF總是LOOPS

有了它,我可以在iOS的UIImageView中導入動畫.gif圖像。這個類完美地工作,但唯一的問題是.gif總是循環。無論我如何導出(我從photoshop導出圖像 - 67幀,重複設置爲「一次」),它將永久循環到UIImageView中。

我輸入我的.gif注意:與這兩條線:

NSURL *url = [[NSBundle mainBundle] URLForResource:@"Loading" withExtension:@"gif"]; 
self.loadingImageView.image = [UIImage animatedImageWithAnimatedGIFData:[NSData dataWithContentsOfURL:url]]; 

很簡單,它的工作原理。 我曾嘗試將animationRepeatCount屬性設置爲1,正如Rob建議的那樣,但這並沒有實現。此外,我也嘗試將UIImageView的animationImages屬性設置爲 gifImage.images,而不是將視圖的圖像屬性 設置爲gifImage。在這種情況下,.gif根本不是動畫。

任何想法如何只玩一次.gif?我可以想到很多技巧(比如設置GIF的最後一幀在一段時間後出現,但如果可能的話,我會首先嚐試一些簡單的方法)。

回答

16

我考了應用程序從該項目,改變了viewDidLoad方法是:

- (void)viewDidLoad { 
    [super viewDidLoad]; 

    NSURL *url = [[NSBundle mainBundle] URLForResource:@"test" withExtension:@"gif"]; 
    UIImage *testImage = [UIImage animatedImageWithAnimatedGIFData:[NSData dataWithContentsOfURL:url]]; 
    self.dataImageView.animationImages = testImage.images; 
    self.dataImageView.animationDuration = testImage.duration; 
    self.dataImageView.animationRepeatCount = 1; 
    self.dataImageView.image = testImage.images.lastObject; 
    [self.dataImageView startAnimating]; 
} 

當應用程序啓動時,它曾經動畫圖像,然後顯示剛剛過去的框架。如果我連接發送[self.dataImageView startAnimating]的按鈕,則點擊該按鈕會再次運行動畫,然後再次停在最後一幀。

確保您按上述方式設置圖像視圖。確保你是而不是將圖像視圖的image屬性設置爲動畫圖像。如果要停止動畫,則必須將圖像視圖的image屬性設置爲非動畫圖像。

+0

謝謝!這個竅門! – jovanjovanovic

+0

羅布,你可以檢查出我的問題使用'UIImage + animatedGIF'在地圖視圖覆蓋http://stackoverflow.com/questions/20558591/animated-gif-not-working-in-mkmapview-overlay-using-mkoverlayrenderer? noredirect = 1#comment30789828_20558591。我無法讓您的類別在疊加層中爲gif製作動畫。 – wigging

+0

@Gavin,這可以工作https://github.com/rishi420/Animated-GIF-iPhone –

1

我已付出「的UIImage,從動畫,GIF」,由「Dreddik」製成,增加了一個委託方法

​​

所以,你知道的時候,動畫將重複和做自己的事在中間每一次重複。您可以統計動畫重複的次數,並在達到的特定數量時停止動畫。

分叉庫是在https://github.com/rishi420/Animated-GIF-iPhone