2012-07-25 75 views
1

我一直在努力奮鬥幾天,試圖獲得最有效/高效的解決方案,將圖像數組加載到.animationImages屬性中,並且能夠隨時更改該數組。這裏是場景: - 我有一個UIImageView - 根據用戶輸入(手機移動,陀螺儀),我會加載一個特定的圖像陣列進行動畫處理。 - 在用戶輸入(觸摸)上播放加載的動畫。UIImageView動畫預加載

現在,使用initWithData在塊上調用的「NSThread detachNewThreadSelector」(僅在某些情況下)在gyroHandler上運行,將圖像數組加載到另一個線程上。其他啓動方法完全失敗。

現在的問題是,當我第一次觸摸(自從當前動畫被加載後)並觸發動畫時,整個事情都會凍結以達到aprox。一秒鐘,然後播放動畫。如果再次觸摸,它會成功播放動畫而不會延遲/凍結。

現在我讀的地方在後臺動畫......我試着使用:

[imgAnimationKey performSelectorInBackground:@selector(startAnimating) withObject:nil]; 

,但結果是一樣的。

我的數組有19個圖像,並且最有可能總是有相同的。 問題是我可能會有更多的5+動畫可以播放,這就是爲什麼我沒有多個UIImageViews。

任何人都知道一種預加載圖像的方法,並避免第一次播放的延遲?或者我可以讓動畫在不同的線程中運行,並避免這種效果(我可能會做錯)?

謝謝!

+0

我減少了圖像質量。它有所改善,但仍有一些延遲。 – SergioM 2012-07-25 05:49:11

回答

0

你可以做的UIImage的類別的類別在後臺線程預加載你的形象,你把它提供給您的UIImageView的圖像屬性之前:

H:

#import <Foundation/Foundation.h> 

@interface UIImage (preloadedImage) 

- (UIImage *) preloadedImage; 

@end 

米:

#import "UIImage+preloadedImage.h" 

@implementation UIImage (preloadedImage) 

- (UIImage *) preloadedImage { 
    CGImageRef image = self.CGImage; 

    // make a bitmap context of a suitable size to draw to, forcing decode 
    size_t width = CGImageGetWidth(image); 
    size_t height = CGImageGetHeight(image); 

    CGColorSpaceRef colourSpace = CGColorSpaceCreateDeviceRGB(); 
    CGContextRef imageContext = CGBitmapContextCreate(NULL, width, height, 8, width * 4, colourSpace, 
                 kCGImageAlphaPremultipliedFirst | kCGBitmapByteOrder32Little); 
    CGColorSpaceRelease(colourSpace); 

    // draw the image to the context, release it 
    CGContextDrawImage(imageContext, CGRectMake(0, 0, width, height), image); 

    // now get an image ref from the context 
    CGImageRef outputImage = CGBitmapContextCreateImage(imageContext); 

    UIImage *cachedImage = [UIImage imageWithCGImage:outputImage]; 

    // clean up 
    CGImageRelease(outputImage); 
    CGContextRelease(imageContext); 

    return cachedImage; 
} 

@end 

使預加載的圖像的調用後臺線程然後在主線程上設置結果。

+0

感謝您的快速響應。我其實沒有嘗試過。我們決定不包括這一點。再次感謝。 – SergioM 2012-07-27 00:39:22

+0

你是否解決了你的問題? – tiguero 2012-07-27 06:35:50

+0

如果是發佈您的答案,以便任何人都可以從中受益:-) – tiguero 2012-07-27 06:36:20