uiimagepickercontrolleroriginalimage内存警告

时间:2014-03-05 07:52:28

标签: ios objective-c xcode memory-leaks uiimagepickercontroller

我有一个集合视图(UICollectionViewController子类),我在其中放置了一个名为“拍照”的UIBarButtonItem。当我点击条形按钮项目时,我的相机打开&我拍了一张照片。在我从相机“使用照片”后的项目中,它将移动到裁剪视图控制器,在我从裁剪视图中选择图像后裁剪图像,图像将放置在我的集合视图中当前控制器。在裁剪视图中,我有两个按钮使用和取消。我的问题是,当我奇怪2,3或4我在控制台收到内存警告然后应用程序崩溃警报“应用程序因内存压力终止”。我一直在使用此代码,当我使用UIImagePickerControllerEditedImage时,如果我使用UIImagePickerControllerOriginalImage则没有问题我会遇到内存问题。

我的问题是提供高质量的图片,而UIImagePickerControllerOriginalImage提供的质量高于UIImagePickerControllerEditedImage。我必须摆脱这个内存问题,因此我使用了这一行[self dismissViewControllerAnimated:YES completion:NULL];但我无法清除记忆警告。

- (IBAction)TakeaPhoto:(id)sender {

[[UIApplication sharedApplication]setStatusBarHidden:FALSE withAnimation:NO];
    gallery=0;
picker1 = [[UIImagePickerController alloc] init];
   picker1.delegate = self;
    self.resizeableCropArea =YES;
    self.cropSize=CGSizeMake(296, 350);
       picker1.sourceType = UIImagePickerControllerSourceTypeCamera;
   [self presentViewController:picker1 animated:YES completion:NULL];
}

- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {

    UIImage *image =  [info objectForKey:
                                  UIImagePickerControllerOriginalImage];

  image_cap = [self imageTemp:image scaledToSize:CGSizeMake(320, 370)];

   dataTemp = UIImageJPEGRepresentation(image,0.0);

    CropViewController *cropController = [[CropViewController alloc] init];

   cropController.sourceImage = [info objectForKey:UIImagePickerControllerOriginalImage];


   Original_img = UIImageJPEGRepresentation(cropController.sourceImage,0.0);

   [original_image addObject:[UIImage imageWithData:Original_img]];

    NSLog(@"source image=%@",cropController.sourceImage);

    cropController.resizeableCropArea = self.resizeableCropArea;

    cropController.cropSize = self.cropSize;

    cropController.delegate = self;

    Cancel_Image= cropController.sourceImage;

     [self dismissViewControllerAnimated:YES completion:NULL];

    [self.navigationController
     pushViewController:cropController animated:YES];
}

1 个答案:

答案 0 :(得分:0)

也许你应该压缩你的JPEG图像。例如,如果您需要图像的大小小于70kb,则可以使用以下代码:

    float compressionRate = 0.90;    // Initial compression rate
    float maxCompressionRate = 0.10; // Max compression rate

    NSData *data = UIImageJPEGRepresentation(outputImage, compressionRate);
    // Our limit of size is MAX_UPLOAD_SIZE (70000) and compressionRate is 0.03f
    while ([data length] > MAX_UPLOAD_SIZE && compressionRate >= maxCompressionRate) {
        compressionRate -= 0.03;
        data = UIImageJPEGRepresentation(outputImage, compressionRate);
    }

注意:您可以使用所需大小(以字节为单位)编辑MAX_UPLOAD_SIZE。