UIImageView 不显示来自 UIImagePickerController 的图像
我正在尝试使用我的应用程序拍照,然后将其显示在我的 .xib 中预定义的 UIImageView 中。我有一个 IBOutlet UIImageView *_foto 链接到 .xib 中的 UIImageView
当我执行以下操作时,拍照后图片不会显示在 UIImageView 中:
- (void) imagePickerController:(UIImagePickerController *)picker didFinishPickingImage:(UIImage *)image editingInfo:(NSDictionary *)editingInfo
{
_foto.image = image;
//Also tried set image, and resizing the image first
[self dismissModalViewControllerAnimated:YES];
}
现在,当我添加代码来创建一个返回图像的新图像视图时从我的选择器中,并将其作为子视图添加到我的视图中,如下所示,发生了一些奇怪的事情:
- (void) imagePickerController:(UIImagePickerController *)picker didFinishPickingImage:(UIImage *)image editingInfo:(NSDictionary *)editingInfo
{
UIImageView *iv = [ [UIImageView alloc] initWithImage:image];
[self.view addSubview:iv];
[iv release];
_foto.image = image;
[self dismissModalViewControllerAnimated:YES];
}
现在图像按预期显示在我视图左上角新创建的图像视图中,而且也显示在我的 _foto 中UIImageView。这让我非常困惑,我不知道发生了什么,我只知道这不是我期望的行为。有谁有任何线索,并希望有一个正确的解决方案来解决这个问题?
I am trying to take a picture with my app and then show it in a UIImageView that is predefined in my .xib. I have a IBOutlet UIImageView *_foto that is linked to the UIImageView in the .xib
When I do the following the picture doesnt show in the UIImageView after taking a picture:
- (void) imagePickerController:(UIImagePickerController *)picker didFinishPickingImage:(UIImage *)image editingInfo:(NSDictionary *)editingInfo
{
_foto.image = image;
//Also tried set image, and resizing the image first
[self dismissModalViewControllerAnimated:YES];
}
Now, when I add code to create a new image view with the image returned from my picker, and add that as a subView to my view like this, something strange happens:
- (void) imagePickerController:(UIImagePickerController *)picker didFinishPickingImage:(UIImage *)image editingInfo:(NSDictionary *)editingInfo
{
UIImageView *iv = [ [UIImageView alloc] initWithImage:image];
[self.view addSubview:iv];
[iv release];
_foto.image = image;
[self dismissModalViewControllerAnimated:YES];
}
Now the image shows in both the newly created image view in the left top corner of my view,as expected, but also in my _foto UIImageView. This has me seriously confused, and I have no idea what is going on, all i know is that it is not the behavior i expected. Does anyone have any clue, and hopefully a proper solution for this problem?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
Artur Ozierański 的回答部分正确。
如果您使用 viewDidLayoutSubviews() 来配置布局,当选择器消失时会调用此函数,因此如果您在 viewDidLayoutSubviews() 中设置默认图像,您将重置回默认图像。
在viewDidLoad中配置默认图片。
Artur Ozierański's answer is partially correct.
If you're using viewDidLayoutSubviews() to configure your layout, this gets called when the picker disappears, so if you're setting the default image in viewDidLayoutSubviews(), you'll reset back to the default image.
Configure the default image in viewDidLoad.
UIImagePickerController 在选取图像时会占用大量设备内存。在
- (void) imagePickerController:(UIImagePickerController *)picker didFinishPickingImage:(UIImage *)image editingInfo:(NSDictionary *)editingInfo
期间,我多次注意到内存警告级别 1 和 2。它可能会导致强制卸载并重新加载视图,再次启动视图控制器的 viewDidLoad 方法。如果您的 _foto 对象是 IBOutlet ,它将从内存中删除,并再次加载所有属性(图像也是如此)的起始值。另外,如果您在 viewDidLoad 中将 image 设置为 nil ,这可能是一个原因。将一些 NSLog 放入 viewDidLoad 中检查是否重新启动。您也可以尝试将捕获的图像放入库中 - 如果库中存在图像,则问题可能出在视图卸载中。
UIImagePickerController grab a lot of device memory while picking an image. During
- (void) imagePickerController:(UIImagePickerController *)picker didFinishPickingImage:(UIImage *)image editingInfo:(NSDictionary *)editingInfo
I've noticed many times memory warnings level 1 and 2. It may cause force unload and reload your view, launch viewDidLoad method of your view controller again. If your _foto object is an IBOutlet it will be removed from memory and load again with start values of all properties (image too). Also if you set image to nil in viewDidLoad it may be a reason.Put some NSLog into viewDidLoad to check out if it is relaunched. Also you may try to put captured image into library - if image exists in library the problem probably is in view unloading.