带有相机或相机胶卷选择器的 iPhone UIImageView 内存警告级别 2

发布于 2024-09-14 21:09:03 字数 936 浏览 5 评论 0原文

我即将完成我的第一个应用程序并放入商店。一切工作正常,内存泄漏几乎完全不存在......除非我使用相机或从相机胶卷中选择图像。

如果用户选择相机与卷轴......相机工作正常......拍照,然后当他们选择“使用”时它崩溃。相机胶卷也是如此。我是个菜鸟,所以如果我搞砸了一些事情,我不会感到惊讶。非常感谢任何帮助/建议......这是代码:

    -(IBAction) getPhoto:(id) sender {
    UIImagePickerController * picker = [[UIImagePickerController alloc] init];
    picker.delegate = self;

    if((UIButton *) sender == choosePhoto) {
        picker.sourceType = UIImagePickerControllerSourceTypeSavedPhotosAlbum;
    } else {
        picker.sourceType = UIImagePickerControllerSourceTypeCamera;
    }

    [self presentModalViewController:picker animated:YES];
    //[picker release];
}
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {
    [picker dismissModalViewControllerAnimated:YES];
    theimageView.image = [info objectForKey:@"UIImagePickerControllerOriginalImage"];
    [picker release];
}

I'm soooooo close to finally finishing my first app to put in the store. Everything works just fine and memory leaks are almost totally nonexistent....except when I'm using the Camera or Selecting an Image from the Camera roll.

If the user chooses the camera vs. the roll....the camera works fine...takes a picture and then when they select "Use" it crashes. Same thing for the camera roll. I'm a noob so if I messed something up it wouldn't surprise me. Any help/suggestions greatly appreciated...here's the code:

    -(IBAction) getPhoto:(id) sender {
    UIImagePickerController * picker = [[UIImagePickerController alloc] init];
    picker.delegate = self;

    if((UIButton *) sender == choosePhoto) {
        picker.sourceType = UIImagePickerControllerSourceTypeSavedPhotosAlbum;
    } else {
        picker.sourceType = UIImagePickerControllerSourceTypeCamera;
    }

    [self presentModalViewController:picker animated:YES];
    //[picker release];
}
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {
    [picker dismissModalViewControllerAnimated:YES];
    theimageView.image = [info objectForKey:@"UIImagePickerControllerOriginalImage"];
    [picker release];
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

如果没有你 2024-09-21 21:09:03

您的问题可能是,由于您使用原始图像,因为它的大小类似于 1400x750(不确定确切的尺寸),所以当您将其设置为要显示的 imageview 的图像时,您可能会耗尽内存......您可能应该将图像大小调整为 320x480 或 480x320 以在图像视图中显示,这可能会解决您的问题。

Your problem might be, since you use the original image, since its something like 1400x750 (not sure about the exact dimensions), you are probably running out of memory when you are setting it as the image of the imageview to be displayed...You should probably resize your image to 320x480 or 480x320 to display it in the image view, that will probably fix your problem.

又爬满兰若 2024-09-21 21:09:03

我突然想到的唯一问题是 UIImagePickerControllerOriginalImage 是一个 NSString 常量,因此您不想将其放在引号中:

theimageView.image = [info objectForKey:UIImagePickerControllerOriginalImage];

但即使该行失败,它只会将 theimageView.image 设置为 nil,这可能不会导致崩溃。您应该至少在 Xcode 控制台中看到一些有关崩溃的更多信息,这会有所帮助。另外,请查看 这个答案

The only issue that jumps out at me is that UIImagePickerControllerOriginalImage is an NSString constant, so you don't want to put it in quotes:

theimageView.image = [info objectForKey:UIImagePickerControllerOriginalImage];

But even if that line were to fail, it would only set theimageView.image to nil which probably shouldn't cause a crash. You should see at least some more info about the crash in the Xcode Console, which will help. Also, check out the tips in this SO answer.

你又不是我 2024-09-21 21:09:03

改变
[选择器解雇ModalViewControllerAnimated:是];



[自我解雇ModalViewControllerAnimated:是];

那应该有效

Change
[picker dismissModalViewControllerAnimated:YES];

to

[self dismissModalViewControllerAnimated:YES];

That should work

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文