【问题标题】:Memory warning when tranfer image picked camera to one view to other将图像选择器相机转移到一个视图到另一个视图时的内存警告
【发布时间】:2013-05-03 07:45:26
【问题描述】:

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

有没有人帮帮我。

提前致谢。

【问题讨论】:

    标签: iphone ios uiimagepickercontroller


    【解决方案1】:

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

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

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

      应该改为:

      UIImage *image = [info objectForKey:@"UIImagePickerControllerOriginalImage"];
      
    • 接下来,你不会在这里释放ImageDisplayViewController_obj 实例:

      // [ImageDisplayViewController_obj release];
      

      取消注释此行。

    内存管理的黄金法则:永远释放你不再需要的对象,不要保留你不需要的任何东西。

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

    其他一些建议:

    【讨论】:

    • @MarOux 感谢您的回复。我这样做了.. 仍然我遇到了一个棘手的问题。因为我正在使用相机点击的图像,该图像在应用程序中接近 2MB。我通过调整大小来尝试它图像到 20X20 它可以工作..但是当我尝试通过 self.frame.size 调整它的大小..它崩溃了
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多