Ios 优化UIImage数组
因此,我正在构建一个应用程序,用户可以拍摄自己的照片,它将这些照片保存到相机卷中,我正在保存对资源URL的引用,以便在应用程序中显示它们。起初,这个模型似乎运行良好,但随着我拍摄越来越多的照片,它开始收到内存警告,最终崩溃。有没有更好的办法 这是我在应用程序启动时加载已保存照片的方式(根据加载的照片数量,应用程序最多可冻结10秒): 和保存照片:Ios 优化UIImage数组,ios,objective-c,data-structures,uiimage,alassetslibrary,Ios,Objective C,Data Structures,Uiimage,Alassetslibrary,因此,我正在构建一个应用程序,用户可以拍摄自己的照片,它将这些照片保存到相机卷中,我正在保存对资源URL的引用,以便在应用程序中显示它们。起初,这个模型似乎运行良好,但随着我拍摄越来越多的照片,它开始收到内存警告,最终崩溃。有没有更好的办法 这是我在应用程序启动时加载已保存照片的方式(根据加载的照片数量,应用程序最多可冻结10秒): 和保存照片: - (void) addImageToPhotos: (UIImage*)image { // Store image at front of
- (void) addImageToPhotos: (UIImage*)image
{
// Store image at front of array
NSMutableArray* temp = [[NSMutableArray alloc] initWithObjects: image, nil];
// load rest of images onto temp array
for (UIImage* image in _photos)
{
[temp addObject: image];
}
_photos = nil;
_photos = [[NSMutableArray alloc] initWithArray: temp];
// [self.photos addObject: image];
[[NSNotificationCenter defaultCenter] postNotificationName: @"PhotosChanged" object: self.photos];
// save to cache
ALAssetsLibrary* library = [[ALAssetsLibrary alloc] init];
[library saveImage: image toAlbum: @kAlbumeName withCompletionBlock:^(NSError *error) {
if (error)
{
NSLog(@"Error saving");
}
}];
}
我认为有两种方法可以优化这个问题
您的项目ARC是否已启用?.bcz我在您的代码中未看到任何手动释放/自动释放池…请查看此。。啊,当然!好主意,我将围绕这个解决方案实施。谢谢:)
- (void) addImageToPhotos: (UIImage*)image
{
// Store image at front of array
NSMutableArray* temp = [[NSMutableArray alloc] initWithObjects: image, nil];
// load rest of images onto temp array
for (UIImage* image in _photos)
{
[temp addObject: image];
}
_photos = nil;
_photos = [[NSMutableArray alloc] initWithArray: temp];
// [self.photos addObject: image];
[[NSNotificationCenter defaultCenter] postNotificationName: @"PhotosChanged" object: self.photos];
// save to cache
ALAssetsLibrary* library = [[ALAssetsLibrary alloc] init];
[library saveImage: image toAlbum: @kAlbumeName withCompletionBlock:^(NSError *error) {
if (error)
{
NSLog(@"Error saving");
}
}];
}