anguish
anguish

Reputation: 458

Xamarin Studio: Add UIimageView to UIView

I want to add a imageview to my view, but the image doesn't appear. Whats wrong with my code? Thank you for Help.

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

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

Upvotes: 1

Views: 4957

Answers (2)

Edwin Lambregts
Edwin Lambregts

Reputation: 408

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);

Upvotes: 1

poupou
poupou

Reputation: 43553

You code snippet is rather short but you should not call iv.Release (); since you'll unbalance the reference counting of the native (ObjC) UIImageView instance.

In fact you almost never have to call this method yourself since Xamarin.iOS has a garbage collector (GC) that will, when disposing the object, automatically call the release selector (just like a retain will be done when creating the managed instance).

Upvotes: 1

Related Questions