2

我正在开发一个类似于 iPhone 中的照片应用程序的应用程序。流程是我通过UIImagePickerController. 当我使用实际图像并将该图像进一步保存到文档时,我的应用程序挂起。我在配置文件工具上看到分配到 19 到 20 mb。如果有任何解决方案。

  1. UIImagepicker从控制器获取图像
  2. 将其保存到文档中。

     -(NSString *)saveImageInDocumentsAtPath:(NSNumber *)number
    {
       NSString *fileName=[NSString stringWithFormat:@"image%@.png",number];
       NSString *imagePath= [[NSString alloc] initWithFormat:@"%@/%@",    [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0],fileName];
       UIImage *image = self.selectedImage.image;
       NSData *imageData = UIImagePNGRepresentation(image);
       [imageData writeToFile:imagePath atomically:YES];
       return imagePath;
    }
    
4

2 回答 2

1

如果我没记错的话,一个典型的图像大约是 3 兆字节(取决于 iPhone 和它的相机)。所以从 19 到 20 并不是不合理的。为什么你从 19 岁开始是另一个问题。查看您是否加载了太多其他图像、大文件等。

如果您使用 AV Foundation 而不是 ImagePicker,则可以使用其他一些选项来处理图像。

您可以将图像异步写入文件系统。

于 2013-03-06T07:08:42.863 回答
1

这看起来像是在代码中检查一些错误的好机会。

为什么不试试这个?

-(NSString *)saveImageInDocumentsAtPath:(NSNumber *)number
{
    NSString *fileName=[NSString stringWithFormat:@"image%@.png",number];
    NSArray * docDirectoryArray = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    if(docDirectoryArray)
    {
        NSString *imagePath= [[NSString alloc] initWithFormat:@"%@/%@",    [docDirectoryArray objectAtIndex:0],fileName];
        UIImage *image = self.selectedImage.image;
        if(image)
        {
            NSData *imageData = UIImagePNGRepresentation(image);
            if(imageData)
            {
                BOOL success = [imageData writeToFile:imagePath atomically:YES];
                if(success)
                {
                    // only return the image path in the success == YES case
                    return imagePath;
                } else {
                    NSLog( @"did not save file to %@", imagePath);
                }
            } else {
                NSLog( @"could not get image data out of the image");
            }
        } else {
            NSLog( @"no image selected");
        }
    }
    // you might want to check in the caller to make certain the imagePath is NULL
    return NULL;
}
于 2013-03-06T07:13:07.570 回答