Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/objective-c/25.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 优化UIImage数组_Ios_Objective C_Data Structures_Uiimage_Alassetslibrary - Fatal编程技术网

Ios 优化UIImage数组

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

因此,我正在构建一个应用程序,用户可以拍摄自己的照片,它将这些照片保存到相机卷中,我正在保存对资源URL的引用,以便在应用程序中显示它们。起初,这个模型似乎运行良好,但随着我拍摄越来越多的照片,它开始收到内存警告,最终崩溃。有没有更好的办法

这是我在应用程序启动时加载已保存照片的方式(根据加载的照片数量,应用程序最多可冻结10秒):

和保存照片:

- (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");
        }

    }];

}

我认为有两种方法可以优化这个问题

  • U应该只保存图像名称字符串而不是保存UIImage对象,然后当需要显示图像时,根据保存的图像名称字符串使用分页来显示图像

  • 您应该使用多线程来处理此长时间任务,建议您使用gcd加载图像名称字符串


  • 您的项目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");
            }
    
        }];
    
    }