0

我在按钮单击时打开相机并在 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];
    }
}

有没有人帮帮我。

提前致谢。

4

1 回答 1

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。

其他一些建议:

于 2013-05-03T08:09:08.290 回答