Iphone 将拾取的图像从一个视图传输到另一个视图时出现内存警告

Iphone 将拾取的图像从一个视图传输到另一个视图时出现内存警告,iphone,ios,uiimagepickercontroller,Iphone,Ios,Uiimagepickercontroller,我打开相机,点击按钮,在uiimage中拍摄图像,然后将uiimage传输到其他视图 但当我这样做4-5次时,我会收到内存警告 以下是我工作的代码:- -(void)imagePickerController:(UIImagePickerController*)picker_camera didFinishPickingMediaWithInfo:(NSDictionary*)info { [picker_camera dismissModalViewControllerAnimated

我打开相机,点击按钮,在uiimage中拍摄图像,然后将uiimage传输到其他视图 但当我这样做4-5次时,我会收到内存警告

以下是我工作的代码:-

-(void)imagePickerController:(UIImagePickerController*)picker_camera didFinishPickingMediaWithInfo:(NSDictionary*)info
{
    [picker_camera dismissModalViewControllerAnimated:YES];
    UIImage *image=[[UIImage alloc] init];
    image=[info objectForKey:@"UIImagePickerControllerOriginalImage"];

    [self Methodcall:image];
    //Image_camera=image;
   // NSAutoreleasePool *apool = [[NSAutoreleasePool alloc] init];
    //printf("first\n");

   // [self performSelector:@selector(Methodcall) withObject:nil afterDelay:1];

    //printf("ok\n");
    //[apool release];

}
-(void)Methodcall:(UIImage *)image{
    ImageDisplayViewController *ImageDisplayViewController_obj=[[ImageDisplayViewController alloc] initWithNibName:@"ImageDisplayViewController" bundle:nil];
    ImageDisplayViewController_obj.image_FRomCamera=image;
    NSLog(@"image===>%@    camera==>%@",image,ImageDisplayViewController_obj.image_FRomCamera);
    [self.navigationController pushViewController:ImageDisplayViewController_obj animated:YES];
   // [ImageDisplayViewController_obj release];
}

-(IBAction)TakePhotoCamera:(id)sender{
    @try
    {
        UIImagePickerController *picker_camera = [[UIImagePickerController alloc] init];
        picker_camera.sourceType = UIImagePickerControllerSourceTypeCamera;
        picker_camera.delegate = self;

        [self presentModalViewController:picker_camera animated:YES];
        [picker_camera release];
    }
    @catch (NSException *exception)
    {
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"No Camera" message:@"Camera is not available  " delegate:self cancelButtonTitle:@"Ok" otherButtonTitles:nil];
        [alert show];
        [alert release];
    }
}
有人帮我吗


提前谢谢。

我想您没有使用ARC(有什么特别的原因吗?)

  • 首先,您正在分配一个从未使用过的UIImage实例:

    UIImage *image=[[UIImage alloc] init];
    image=[info objectForKey:@"UIImagePickerControllerOriginalImage"];
    
    应改为:

    UIImage *image = [info objectForKey:@"UIImagePickerControllerOriginalImage"];
    
  • 接下来,您不会在此处释放
    ImageDisplayViewController\u obj
    实例:

    // [ImageDisplayViewController_obj release];
    
    取消对此行的注释

内存管理的黄金法则:始终释放您不再需要的对象,并且不保留您不需要的任何内容。

如果您不熟悉Obj-C和/或内存管理,我强烈建议您使用ARC

其他一些建议:

  • -不要命名变量
    picker\u camera
    (使用
    pickerCamera
    ),或
    ImageDisplayViewController\u obj
    (使用
    imageController
    imageVC

@MarOux感谢您的回复。我做到了。但我还是遇到了崩溃问题。因为我使用的是摄像头点击的图像,在应用程序中几乎是2MB。我尝试将图像大小调整为20X20,它可以工作。但是当我尝试按self.frame.size调整图像大小时,它崩溃了