2011-02-27 95 views
0

我創建了一個名爲myclass的基於視圖的項目。在imageview中顯示圖像時出現問題

我添加了另一個UIviewcontroller子類給它,命名爲webdata。

在webdataviewcontroller.m我從網絡服務器獲取圖像。

我需要從webdataViewController.m

這個我在webdataViewController.m代碼myclassViewController.m顯示在imageview的這個形象是

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> 

爲什麼我得到null爲obj.homeimage.image,我沒有得到任何一個請幫助我。

謝謝你的愛戴。

(讓我補充評論,如果問題是不理解)

+0

您應該使用'stringByAppendingPathComponent:'來​​創建路徑。 – fabian789 2011-02-27 09:36:32

回答

1

你確定obj.homeimage不是零嗎?如果它是零,那麼obj.homeimage.image = ...會導致將圖像屬性設置爲零對象,即「無」。

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 -ed一個新的myclassViewController但它的變量都沒有初始化,所以你只是消息零對象可能。這就是爲什麼你的代碼沒有崩潰但仍然沒有響應。這又是一個猜測,但它可能是問題所在。

如果您爲myclassViewController提供代碼,則可以使用更多信息進行編輯。