2013-08-30 91 views
1

我想將imageview添加到我的視圖中,但圖像未顯示。 我的代碼有什麼問題?謝謝你的幫助。Xamarin Studio:將UIimageView添加到UIView

...Constructor(UIImage _pickedImage...... 

UIImageView iv = new UIImageView (this.Bounds); 
iv.Image = this.pickedImage; 
this.AddSubview (iv); 
iv.Release(); 

回答

1

您的代碼片段很短,但你應該通話iv.Release();因爲你不平衡本地(ObjC)UIImageView實例的引用計數。

事實上,你幾乎從未已自行調用此方法,因爲Xamarin.iOS有一個垃圾收集器(GC)會,處置的對象時,自動調用release選擇(就像一個retain將完成時創建託管實例)。

1

今天我遇到了這個問題,發現這個問題還沒有正確的答案,但如何解決它。所以爲了將來的參考,我會解釋我是如何解決它的。

我遍歷包含圖像的列表,如item所示。 首先,創建的UIImageView

UIImageView imageView = new UIImageView(); 

然後我取的圖像的字節數組,並將其轉換爲一個的UIImage

UIImage image = Utilities.ToImage(item.ImageBytesArray); 

然後,設置的UIImageView與UIImage的

imageView.Image = image; 

接下來是解決了我的主要問題,沒有出現。所以,我們要得到屏幕尺寸,並將其存儲在screen

var screen = UIScreen.MainScreen.Bounds; 

設置的UIImageView的框架。這創建了可以看到的UIImageView。爲了不重疊其他內容,我必須從頂部抵消400。
CGRect(xOffset, yOffset, width, height)

imageView.Frame = new CoreGraphics.CGRect(0, 400, screen.Size.Width, 300); 

我用的是滾動型的,所以我把它添加到這一點。也可以是this

scrollView.Add(imageView); 

的UIImageView的ImageView =新的UIImageView();
UIImage image = Utilities.ToImage(item.ImageBytesArray);
imageView.Image = image;
var screen = UIScreen.MainScreen.Bounds;
imageView.Frame = new CoreGraphics.CGRect(0,400,screen.Size.Width,300);
scrollView.Add(imageView);

相關問題