圖片幀播放性能優化及音頻播放路徑

/**

 *  圖片幀播放

 */

-(void)animationWithTomImageName:(NSString *)imageName andWithCount:(int)count

{

    if ([self.tomImage isAnimating]) {

        return;//如果有動畫在執行其他動畫就不能執行

    }

    NSMutableArray *imageArray = [NSMutableArray array];

    for (int i = 0; i < count; i ++) {

        NSString *name = [NSString stringWithFormat:@"%@_%02d.jpg",imageName,i ];

        //UIImage *image = [UIImage imageNamed:name];(性能優化前的取圖片方法)

        

        //性能優化後的取圖片方法

        NSString * path = [[NSBundle mainBundle]pathForResource:name ofType:nil];

        UIImage *image = [UIImage imageWithContentsOfFile:path];

        [imageArray addObject:image];

    }

    self.tomImage.animationImages = imageArray;

    self.tomImage.animationDuration = count * 0.06;

    self.tomImage.animationRepeatCount = 1;

    [self.tomImage startAnimating];

    //釋放資源,播放完圖片後,釋放圖片數組,優化內存

    [self.tomImage performSelector:@selector(setAnimationImages:) withObject:nil afterDelay:count * 0.06 + 0.3];

}

/**

 *  音頻播放

 */

-(void)playerMusicWithName:(NSString *)name

{

    //音頻路徑

    NSString *path = [[NSBundle mainBundle]pathForResource:name ofType:nil];

    //轉化成url

    NSURL *url = [NSURL fileURLWithPath:path];

    //初始化player

    AVPlayer *p = [[AVPlayer alloc]initWithURL:url];

    self.play = p;

    //播放

    [self.play play];

    

}


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章