简体   繁体   English

UIImagePickerController不加载图像

[英]UIImagePickerController does not load image

This seems like a relatively simple issue but it has me stumped. 这似乎是一个相对简单的问题,但我感到很困惑。 Any help / insight is appreciated. 任何帮助/见解表示赞赏。

Problem: 问题:

I have a UIImagePickerController button on my screen and would like to initially load with a custom image until the user selects an image from the camera roll (the custom image is just a + sign). 我的屏幕上有一个UIImagePickerController按钮,希望最初加载自定义图像,直到用户从相机胶卷中选择一个图像为止(自定义图像只是一个+号)。 However the UIImagePickerController button appears blank until the user clicks it for the first time. 但是,UIImagePickerController按钮将显示为空白,直到用户第一次单击它为止。 After the first click, it works fine but for some reason it does not initialize with the default image. 第一次单击后,它可以正常工作,但是由于某种原因,它没有使用默认图像初始化。 I have pasted my code from viewDidLoad method in the controller. 我已经将代码从viewDidLoad方法粘贴到控制器中。

Code : 代码:

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);  
NSString *documentsPath = [paths objectAtIndex:0];
NSString *filePath = [documentsPath stringByAppendingPathComponent:[self.detailItem cardName]];
filePath = [filePath stringByAppendingPathExtension:@"png"];
NSData *pngData = [NSData dataWithContentsOfFile:filePath];
imgPickerButton.imageView.image = [UIImage imageWithData:pngData];

So what am I doing wrong? 那我在做什么错? Any insight is appreciated and thanks in advance for your help. 感谢您提供任何见解,并在此先感谢您的帮助。

这说明了情节提要中屏幕的设计视图

在用户单击按钮之前视图如何显示的屏幕截图

Debug it? 调试吗?

//Your Code

//Debug it with muscle
NSLog(@"\n**** I Love Debug Statements! *****\ndocumentsPath: %@\n filePath: %@\ndetailItem: %@\ndefault image: %@", documentsPath, filePath, self.detailItem, imgPickerButton.imageView.image);

My guess is that your "self.detailItem" is nil when you try to call this the first time... 我的猜测是,当您第一次尝试调用此函数时,您的“ self.detailItem”为零...

from the picture it looks like this is a part of a tableview? 从图片看起来这是表视图的一部分吗? If this is the case then the tableview cell is getting loaded before your above code is 如果是这种情况,那么在您上面的代码被加载之前,将加载tableview单元格

Ok figured it out!! 确定了! I was setting the image for the UIButton in the viewDidLoad method. 我在viewDidLoad方法中设置UIButton的图像。 Which is a method called after the view is drawn. 绘制视图后调用的方法。 Hence the default image was not appearing until the first click, at which time the UIButton is redrawn. 因此,默认图像直到第一次单击后才出现,此时UIButton被重绘。 To solve this issue I moved the code to awakeFromNib method and voila!! 为了解决这个问题,我将代码移至awakeFromNib方法,瞧! it works fine. 它工作正常。 The default image loads smoothly... 默认图像平滑加载...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM