带有相机或相机胶卷选择器的 iPhone UIImageView 内存警告级别 2
我即将完成我的第一个应用程序并放入商店。一切工作正常,内存泄漏几乎完全不存在......除非我使用相机或从相机胶卷中选择图像。
如果用户选择相机与卷轴......相机工作正常......拍照,然后当他们选择“使用”时它崩溃。相机胶卷也是如此。我是个菜鸟,所以如果我搞砸了一些事情,我不会感到惊讶。非常感谢任何帮助/建议......这是代码:
-(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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您的问题可能是,由于您使用原始图像,因为它的大小类似于 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.
我突然想到的唯一问题是
UIImagePickerControllerOriginalImage
是一个NSString
常量,因此您不想将其放在引号中:但即使该行失败,它只会将
theimageView.image
设置为nil
,这可能不会导致崩溃。您应该至少在 Xcode 控制台中看到一些有关崩溃的更多信息,这会有所帮助。另外,请查看 这个答案。The only issue that jumps out at me is that
UIImagePickerControllerOriginalImage
is anNSString
constant, so you don't want to put it in quotes:But even if that line were to fail, it would only set
theimageView.image
tonil
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.改变
[选择器解雇ModalViewControllerAnimated:是];
到
[自我解雇ModalViewControllerAnimated:是];
那应该有效
Change
[picker dismissModalViewControllerAnimated:YES];
to
[self dismissModalViewControllerAnimated:YES];
That should work