0

我正在开发一个应用程序,我需要在其中捕获多个图像并将它们存储在一个文件夹中。当图像超过时,应用程序崩溃。另外,在拍摄图像时,iPhone 速度会变慢一点。请任何人都可以帮助我。

捕获图像的代码

UIImagePickerController *picker=[[UIImagePickerController alloc]init];
picker.delegate=self;   
picker.sourceType=UIImagePickerControllerSourceTypeCamera;

[self.navigationController presentModalViewController:picker animated:YES];

将图像添加到文件中的代码

NSString  *pngImagePath =
[[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,
NSUserDomainMask, YES) lastObject]
stringByAppendingPathComponent:[NSString
stringWithFormat:@"%@.png",imageName]];

    [dataImage writeToFile:pngImagePath atomically:YES];
4

2 回答 2

0

只需尝试一次@autoreleasepool,可能是由于一些内存泄漏。@autoreleasepool 在某种程度上保持..

 @autoreleasepool
    {
      // any allocation for UIImage if u have just bring inside the autoreleasepool
        NSString  *pngImagePath =
        [[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,
        NSUserDomainMask, YES) lastObject]
        stringByAppendingPathComponent:[NSString
        stringWithFormat:@"%@.png",imageName]];
        [dataImage writeToFile:pngImagePath atomically:YES];
    }
于 2013-04-15T11:27:29.023 回答
0

在这里,以这段代码为例,在将图像显示在屏幕上之前调整它们的大小:

+ (UIImage*)imageWithImage:(UIImage*)image
          scaledToSize:(CGSize)newSize;
{
    UIGraphicsBeginImageContext( newSize );
    [image drawInRect:CGRectMake(0,0,newSize.width,newSize.height)];
    UIImage* newImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();

    return newImage;
}

请注意,在将图像保存到磁盘之前,您还应该缩小图像:

UIImage *image = YOUR_IMAGE;
NSData *imageData = UIImageJPEGRepresentation(image, .06); // Here's the level of compression
于 2013-04-15T11:14:13.903 回答