将图像选取器摄像机传输到另一个视图时的内存警告

时间:2013-05-03 07:45:26

标签: iphone ios uiimagepickercontroller

我按下按钮打开相机并拍摄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];
    }
}

是否有人帮助我。

提前致谢。

1 个答案:

答案 0 :(得分:0)

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

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

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

    应更改为:

    UIImage *image = [info objectForKey:@"UIImagePickerControllerOriginalImage"];
    
  • 接下来,您不会在此处发布ImageDisplayViewController_obj个实例:

    // [ImageDisplayViewController_obj release];
    

    取消注释此行。

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

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

其他一些建议: