批量本地图片视频同时加载的问题

标签: iOS Obj-C AVPlayer UITableView UICollectionView


最近做一个类似照片应用的照片视频浏览器,结合 UITableViewUICollectionView 将自己录制的视频罗列。

实现过程中有几个点需要注意:

  • 显示多个图片时会造成View 的卡顿,加载图片时需要将[UIImage imageNamed:imageName]更改为[UIImage imageWithContentsOfFile:imagePath] 的方式

  • 保存图片到 APP 中时会造成内存的递增,这是因为通过UIImagePNGRepresentation()方法保存PNG格式图片时,由于ARC机制,会产生大量临时的autorelease对象,需要等待runloop的autoreleasepool销毁时才能销毁这些对象。需要使用 ImageIO 的方式来存储图片

+ (void)saveImage:(CGImageRef)image directory:(NSString*)directory filename:(NSString*)filename  {
    @autoreleasepool {
        CFURLRef url = (__bridge CFURLRef)[NSURL fileURLWithPath:[NSString stringWithFormat:@"%@/%@", directory, filename]];
        CGImageDestinationRef destination = CGImageDestinationCreateWithURL(url, kUTTypePNG, 1, NULL);
        CGImageDestinationAddImage(destination, image, nil);
        
        if (!CGImageDestinationFinalize(destination))
            NSLog(@"ERROR saving: %@", url);
        
        CFRelease(destination);
        CGImageRelease(image);
    }
}

编辑视频时,会选择多个视频,需要将选择的视频进行预览播放。在使用AVPlayer来播放视频时,AVPlayer只能创建16个对象,最多只能支持16个视频的同时播放。当视频不需要继续播放后,需要将它的 AVPlayerItem置为 nil,否则之前播放过的视频也会算在16个视频之内。


【参考】

  1. UIImage存为PNG图片内存增长
  2. Putting a UICollectionView in a UITableViewCell

你可能感兴趣的:(批量本地图片视频同时加载的问题)