crush the height and width of the initWithFrame image size - ios

I have a small problem with a UIImageView, I try to place an image dynamically so I start like this
self.picture_view = UIImageView.alloc.initWithFrame (CGRectMake (0, 16,45, 46))
but I want to take my frame size of each image I passed him.
So I do like this.
picture_frame = picture_view.frame;
picture_frame.size = picture_view.size;
picture_view.frame = picture_frame;
but when I do this
NSLog (picture_frame.size.inspect)
it gives me 45, 46 for each image,
So how recovered the image size and frame overrider for me that shows the correct size
thank you in advance.
PS: I done well picture_view.image = UIImage.imageNamed (my_picture)

You are actually setting the ImageViews frame to the frame of itself. You need to be doing it based on the actual UIImage. You can do this automatically by initing the view with the image.
Ex.
UIImageView *pictureView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"whatever.png"]];
//The size is already set correctly, but if you want to change the x or y origin do like so
pictureView.frame = CGRectMake(0, 16, pictureView.frame.size.width, pictureView.frame.size.height);

Edit: This answer is written in Objective-C, but the core idea should translate to rubymotion very easily.
If you are trying to create a UIImageView and then resize it to fit the UIImage you assign to it, you should try something like this:
Create an imageView with an image:
UIImage *image = [UIImage imageNamed:#"myPicture.png"];
UIImageView *imageView = [[UIImageView alloc] initWithImage:image];
Change the image, and resize the imageView to fit:
UIImage *anotherImage = [UIImage imageNamed:#"anotherPicture.png"];
imageView.image = anotherImage;
CGRect imageViewFrame = imageView.frame;
imageViewFrame.size = anotherImage.size;
imageView.frame = imageViewFrame;

Related

How to fit an image on a uiview programmatically

I added a uiview as a subview on a view controller programmatically (called contentView). I also added an image on that uiview programmatically. Every time I run the app on the iPad the image is stretched! How do I fix the image so that it fits the iPad screen but doesn't stretch the image? I know the drawInRect is what is stretching the image. So how do I fix that?
Here is my code:
UIGraphicsBeginImageContextWithOptions(self.contentView.bounds.size, self.contentView.opaque, 0.0);
[[UIImage imageNamed:#"menu image 2.JPG"] drawInRect:self.contentView.bounds];
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
UIImageView *imageView = [[UIImageView alloc] initWithImage:image];
[self.contentView addSubview:imageView];
UIImageView has a property called contentMode which determines how the image layout is handled in the view context. contentMode is of type UIViewContentMode.
The default value is UIViewContentModeScaleToFill which stretches the image without respecting the aspect ratio. I am assuming it is the changing aspect ratio that is causing the issue.
If you wish to scale the image, but keep the aspect ratio, you have two options:
UIViewContentModeScaleAspectFit: This will show the image scaled to fill the view, but not clip any contents (if the aspect ratio doesn't match view size, it will show either horizontal or vertical bands)
UIViewContentModeScaleAspectFill: This will scale the image to fill the view entirely, without any bands - this will result in content being clipped if the image ratio doesn't match the view ratio.
To set the contentMode on the image view in Objective-C, use the following syntax:
UIImage *image = [UIImage imageNamed:#"menu image 2.JPG"];
UIImageView *imageView = [[UIImageView alloc] initWithImage:image];
imageView.contentMode = UIViewContentModeScaleAspectFill;
...
You should not need to use the custom context drawing for this to work anymore (thanks to Losiowaty for asking me about this).
Change code as below
UIImage *image = [UIImage imageNamed:#"menu image 2.JPG"];
UIImageView *imageView = [[UIImageView alloc] initWithImage:image];
imageView.contentMode = UIViewContentModeScaleAspectFit;
[self.contentView addSubview:imageView];
I guess your self.contentView.bounds is not having the same aspect ratio as your menu image 2.JPG . For an experiment, please try looking at the menu image 2.JPG's resolution. For example if it is 300x150, it's aspect ratio is (300/150 = 2:1). Set your self.contentView.bounds to this aspect ratio and draw the image and check the results. It will not stretch.
Please add this Single Line of code in your project
yourImage .contentMode = UIViewContentModeScaleAspectFit;

How do I make a UIImageView not be wider than the view it's contained in?

I set the image of a UIImageView to an image that is 1024x1024, and as a result a lot of the image is not visible, especially width wise, and cut off the edges of the screen.
I tried using clipsToBounds:
UIImageView *imageFromLink = [[UIImageView alloc] initWithImage:responseObject];
imageFromLink.clipsToBounds = YES;
[darkOverlayView addSubview:imageFromLink];
But it doesn't seem to do anything, and the image is still too big for the view.
You're going to want to change the contentMode property for the desired effect. UIViewContentModeScaleAspectFill is most likely what you'll want.
You have the set the image view's frame to the size you want. By default it will be the size of the image.
UIImageView *imageFromLink = [[UIImageView alloc] initWithImage:responseObject];
imageFromLink.contentMode = UIViewContentModeScaleAspectFit;
imageFromLink.frame = CGRectMake(0, 0, desiredWidth, desiredHeight);
[darkOverlayView addSubview:imageFromLink];
Also set the contentMode so the image is scaled properly in the smaller image view.
You should set the imageview's frame equal to the container's bounds.
UIImageView *imageFromLink = [[UIImageView alloc]initWithImage:responseObject];
imageFromLink.contentMode = UIViewContentModeScaleAspectFit;
imageFromLink.frame = darkOverlayView.bounds;
[darkOverlayView addSubview:imageFromLink];

How to fill up an UIImageView by another image?

I would like to fill up an empty UIImageView by another Image.However the two pictures have not the same width, Height and shapes.So what I should do is to scale the second image until it fills up all the area and it should be displayed only there.My problem is how should I proceed.For this moment I can only put the image into the empty area without filling it up.Any idea please ?
please set clipsToBound=YES
UIImageView *img = [[UIImageView alloc]init];
img.image = [UIImage imageNamed:#"yourimage"];
img.contentMode = UIViewContentModeScaleAspectFill;
img.clipsToBounds = YES;
Simply set the content mode to UIViewContentModeScaleAspectFill, as H2CO3 suggested. Try this.
yourImage.contentMode = UIViewContentModeScaleAspectFill;
img.contentMode=UIViewContentModeScaleAspectFill;
This can solve your problem.

uitableview.backgroundView image dimension is compressing/distorting

I'm building an iphone app.
I have a tableview and i set a background image like so:
UIImageView * bg = [[UIImageView alloc] initWithImage:somebackgroundimage];
CGRect framebg = bg.frame;
framebg.origin.y = -64;
framebg.origin.x = 0;
framebg.size.width = 320;
framebg.size.height = 480;
[bg setFrame:framebg];
[_tblview setBackgroundView:bg];
no matter what framebg.size.width or framebg.size.height I set, the image always appears distorted or compressed with a size of maybe 320 width and 400 height. It is almost as if the app will forcefully resize the image to fit between the top navigation bar and my bottom tab bar.
How do I force tableview background image to be of 320px width by 480px height?
Also it seems the origin.y and origin.x aren't being respected either.
I know this question is a bit old, but I thought i would share what I was able to use to get around a similar issue (using setContentMode for the image view):
UIImageView *bg = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"background.png"]];
[bg setContentMode:UIViewContentModeScaleAspectFit];
self.tableView.backgroundView = bg;
Hope this helps.

Creating a UIImageVIew with the exact size of an image

How is the size detected from an image and then use that size to create a UIImageView, perfectly fitting the image inside?
Just create the UIImageView like this:
UIImageView *aImageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"yourImage.png"]];
So the ImageView has the size of the image. From the Documentation of UIImageView:
This method adjusts the frame of the receiver to match the size of the specified image. It also disables user interactions for the image view by default.
I hope my answer helps you. :-D
Sandro
Here no need to set the frame size when we use the image in imageView
UIImageView *img = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"image.png"]];
ImageView will automatically takes the size of image.

Resources