有时 UIImageView 似乎拒绝使用 iPhone 相机拍摄的图像
这很奇怪,因为这个错误不会一直发生...
我有以下代码:
- (IBAction)getPhoto:(id)sender {
UIImagePickerController * picker = [[UIImagePickerController alloc] init];
picker.delegate = self;
#if TARGET_IPHONE_SIMULATOR
picker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
#else
picker.sourceType = UIImagePickerControllerSourceTypeCamera;
#endif
[self presentModalViewController:picker animated:YES];
}
使用相应的委托选择器
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {
UIImage *image = [[info objectForKey:@"UIImagePickerControllerOriginalImage"] imageByScalingToSize:CGSizeMake(480, 320)];
[[self imageView] setImage:image];
[picker dismissModalViewControllerAnimated:YES];
}
奇怪的是,有时图像出现在 imageView 中,有时则没有。我已经打电话
(gdb)po UIImagePNGRepresentation(图像)
紧接着 setImage 并将输出填充到文件中。该文件是完美的 png。
有人经历过同样的事情吗?有没有人找到解决方案?
预先感
谢马克斯
it is very strange, because this error doesn't happen all the time...
I have the following code:
- (IBAction)getPhoto:(id)sender {
UIImagePickerController * picker = [[UIImagePickerController alloc] init];
picker.delegate = self;
#if TARGET_IPHONE_SIMULATOR
picker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
#else
picker.sourceType = UIImagePickerControllerSourceTypeCamera;
#endif
[self presentModalViewController:picker animated:YES];
}
with the corresponding delegated selector
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {
UIImage *image = [[info objectForKey:@"UIImagePickerControllerOriginalImage"] imageByScalingToSize:CGSizeMake(480, 320)];
[[self imageView] setImage:image];
[picker dismissModalViewControllerAnimated:YES];
}
strange thing is, somtimes the image appears in imageView and sometimes not. I have called
(gdb) po UIImagePNGRepresentation(image)
right after setImage and stuffed the output into a file. The file is a perfect png.
Has anyone experienced the same? Has anyone found a solution for it?
Thanks in advance
Max
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
听起来你的内存不足了。
我认为您应该在使用
presentModalViewController:
将 UIImagePickerController 放在屏幕上后释放它。(它将由呈现它的视图控制器为您保留)
Sounds like you are running out of memory.
I think you are supposed to release the
UIImagePickerController
after putting it on screen withpresentModalViewController:
.(It will be retained for you by the view controller that is presenting it)
解决方案是:
拍照后,系统立即调用控制器中收到的MemoryWarning。但控制器本身仍然保留。当按下时,将执行委托选择器并设置 imageView 中的图像。但在关闭模态视图后,系统会从笔尖重新初始化视图。因此,笔尖中的图像会覆盖委托选择器中设置的图像。
解决方案是将 didReceiveMemoryWarning 中所有文本字段和视图的内容存储在字典中,并将图像存储在实例变量中,并在 viewDidLoad 中重置它们(如果存在)。
The solution is:
Right after taking the picture the receivedMemoryWarning in the controller is called by the system. But the controller itself remains retained. When pressing use the delegate selector is performed and the image in imageView is set. But after dismissing the modal view the system reinitializes the view from the nib. So the image from nib overwrites the image set in the delegate selector.
Solution is to store the contents of all text Fields and views in didReceiveMemoryWarning in a Dictionary, and storing the image in a instance variable and resetting them in viewDidLoad if they are present.
是的,这就是对我有用的解决方案。使用 didreceivememorywarning 将所有文本字段和 uiimage 数据存储到手机。在 pickerdidfinish 上,我还将图像保存到手机中。 Onload 我检查手机中是否存在该图像文件名,如果存在则加载它。基本上,iPhone 操作系统会关闭您的应用程序并重新打开它。将一些 NSLog 放入 viewDidLoad 中,您会注意到在图像拍摄过程中会调用它。该方法应该只被调用一次。如果再次调用它,则意味着您的应用程序已死亡并重新启动。这是一个内存问题。马力不够。我在 iPhone 3GS 上没有看到这个问题
Yes that's the solution that worked for me. Using didreceivememorywarning to store all textfield and uiimage data to the phone. On pickerdidfinish I also save the image to the phone. Onload I check if that image file name exists in the phone, if so load it. Basically the iPhone os is closing your app and reopennkng it. Put some NSLog in viewDidLoad and you will notice that gets called during the image taking process. That method is only supposed to get called once. If it is getting called Again then it means your application died and restarted. It is a memory issue. Not enough horse power. I don't see this issue on the iPhone 3GS
我什至在 3GS 上也遇到这个问题。但我可以通过在设置 imageView.image 之前释放 UIImagePickerViewController 来解决这个问题
我想这释放了几个字节。也许这个信息有帮助。
I had this problem even on the 3GS. But I could solve it, by releasing the UIImagePickerViewController before setting the imageView.image
I guess this freed a couple of bytes. Maybe this info helps.