xcode 带有相机或相机胶卷选择器内存警告级别 2 的 iPhone UIImageView
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3503915/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
iPhone UIImageView with Camera or Camera Roll Picker Memory Warning Level 2
提问by Indy-Jones
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];
}
采纳答案by Daniel
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.
您的问题可能是,因为您使用原始图像,因为它类似于 1400x750(不确定确切尺寸),所以当您将其设置为要显示的图像视图的图像时,您可能会耗尽内存......您可能应该将图像大小调整为 320x480 或 480x320 以在图像视图中显示它,这可能会解决您的问题。
回答by Daniel Dickison
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:
唯一让我想到的问题是这UIImagePickerControllerOriginalImage
是一个NSString
常数,所以你不想把它放在引号中:
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.
但即使那条线出现故障,它也只会设置theimageView.image
为nil
可能不会导致崩溃的位置。您应该至少在 Xcode 控制台中看到更多有关崩溃的信息,这将有所帮助。另外,请查看此 SO answer 中的提示。
回答by Devster101
Change
[picker dismissModalViewControllerAnimated:YES];
to
[self dismissModalViewControllerAnimated:YES];
That should work
更改
[pickerdismissModalViewControllerAnimated:YES];
到
[自我解雇ModalViewControllerAnimated:YES];
那应该工作