在imageview中显示图像时出现问题

时间:2011-02-27 09:06:14

标签: iphone

我创建了一个名为myclass的基于视图的项目。

我向它添加了另一个UIviewcontroller子类,名为webdata。

在webdataviewcontroller.m中,我从网络服务器获取图像。

我需要在webcataViewController.m的myclassViewController.m中的imageview上显示这个图像

这个我在webdataViewController.m中的代码是

docDir = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
    imagepath = [NSString stringWithFormat:@"%@/image.png",docDir];
    logopath = [NSString stringWithFormat:@"%@/logo.png",docDir];

myclassViewController *obj = [[myclassViewController alloc]init];
obj.homeimage.image = [UIImage imageWithContentsOfFile:imagepath];

但是没有显示图像,我尝试在控制台中打印图像

NSLog(@"image to display %@",obj.homeimage.image);
NSLog(@"image to display %@",[UIImage imageWithContentsOfFile:imagepath]);    

但我得到这样的

image to display (null)
 image to display <UIImage: 0x6f614b0>

为什么我为obj.homeimage.image获取null,我没有得到任何人可以帮助我。

感谢你。

(如果问题不可理解,请加我评论)

2 个答案:

答案 0 :(得分:1)

为什么不向我们提供myclassViewController的代码?

此外,你在大写等方面也犯了很多错误。这使你很难阅读你的代码并且它很麻烦。

myClassViewController应为MyClassViewController webdataViewController应为WebDataViewController

你应该使用驼峰式变量名称 homeimage应为homeImage imagepath应为imagePath

你说你子类 myclassViewController,我认为你的意思是你把它添加为子视图。如果是这样,当您执行代码时:

myclassViewController *obj = [[myclassViewController alloc]init];

您正在创建一个新的myclassViewController,而不是引用父视图控制器。您需要一个指向myclassViewController实例的指针,该实例将webdataViewController添加为子视图,并将图像添加到其属性中。另外我猜这里有你的问题,你正在创建一个新的myclassViewController空实例,因此你没有初始化你的ivars。 (除非你在-init方法中这样做,但我会怀疑)。所以你alloc - ed和init编辑了一个新的myclassViewController,但没有一个变量被初始化,所以你可能只是消息nil对象。这就是为什么你的代码没有崩溃但仍没有响应的原因。再次这是一个猜测,但它可能是问题。

如果您提供myclassViewController的代码,我可以编辑更多信息。

答案 1 :(得分:1)

你确定obj.homeimage不是零吗?如果它是nil那么obj.homeimage.image = ...将导致将image属性设置为nil对象,即“无”。