1

我想在我的视图中添加一个图像视图,但图像没有出现。我的代码有什么问题?谢谢你的帮助。

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

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

2 回答 2

1

您的代码片段相当短,但您不应该调用iv.Release ();因为您将不平衡本机(ObjC)UIImageView实例的引用计数。

事实上,您几乎不必自己调用此方法,因为 Xamarin.iOS 有一个垃圾收集器 (GC),它将在处理对象时自动调用release选择器(就像retain在创建托管实例时所做的一样)。

于 2013-08-30T15:23:38.843 回答
1

I ran into this problem today, and found that this question had no proper answer yet on how to solve it. So for the sake of future references, I will explain how I solved it.

I loop through a list that contain images, depicted as item. First, create the UIImageView

UIImageView imageView = new UIImageView();

Then I take the byte-array of the image, and convert it to an UIImage

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

Then, set the UIImageView with the UIImage

imageView.Image = image;

Next up is what solved the main issue for me, not showing up. So, we're going to get the screensize and store it in screen

var screen = UIScreen.MainScreen.Bounds;

Set the frame of the UIImageView. This creates the UIImageView that can be seen. My offset from the top had to be 400 in order to not overlap other content.
CGRect(xOffset, yOffset, width, height)

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

I use a scrollview, so I add it to that. Can also be this

scrollView.Add(imageView);

UIImageView imageView = new 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);

于 2015-06-23T15:12:12.470 回答