UIImage retutns wrong width - ios

I have a UIImageView and I put it in a toolbar. When I run it on the simulator, the image gets stretched in. Meaning the width somehow gets less than the toolBar. Here's my code:
UIImageView *imageNav = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0,
nav.frame.size.width, nav.frame.size.height)];
nav.image = [UIImage imageNamed:[imageTop objectAtIndex:0]];
This is the output.
http://postimg.org/image/rjirnmiyx/
The yellow is the toolbar and the green thing is the image. It does not fill the toolbar.
Update
Whatever I do, it gets scaled off at the right side. (Like the image above.)
I tried all kinds of content modes and it all came down to one thing. Something does get ruined at the right hand side.
Update 2
I figured out the problem! The problem is that the UIImageView does not have a constraint. Can someone please help me add a constraint?

you can try this
[imageNav sizeToFit];

UIImage *image = [UIImage imageNamed:[imageTop objectAtIndex:0]]
UIImageView *imageNav = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0,
image.size.width, image.size.height)];
imageNav.image = image;
Hope above code snippet helps..

Related

Xcode: Image becomes filled with color on UIBarItem while scaling, function initeWithImage(UIImage)

please, help me to find some solution to the following problem. IOS, Xcode.
I have UIBarItem component - the button that switches languages on the toolbar. The bar has function initeWithImage(UIImage). The problem is when i am trying to scale the image it becomes filled with color...The picture is too big for the toolbar. I have changed the size of the picture manually, but it looks ugly (i need scaling).
I have tried to solve the problem:
1. trough Scale
2. through Frame size
3. and variations of the two above :)
Some code:
self initWithImage:image //
style: UIBarButtonItemStyleBordered //UIBarButtonItemStylePlain
target:target
action:action
UIImage *image;
image = [[UIImage imageNamed:#“image.png"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
-(UIImage )imageWithImages:(UIImage )image scaledToSize:(CGSize)newSize {
//UIGraphicsBeginImageContext(newSize);
// In next line, pass 0.0 to use the current device's pixel scaling factor (and thus account for Retina resolution).
// Pass 1.0 to force exact pixel size.
UIGraphicsBeginImageContextWithOptions(newSize, NO, 0.0);
[image drawInRect:CGRectMake(0, 0, newSize.width, newSize.height)];
UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return newImage;
}
Please, help me with this. Quite simple but tricky task. I have already spent several hours with that... Thank you in advance :)
You should not use initWithImage method.
If you want to display the exact image you should use initWithCustomView method.
For example:
UIImageView *imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"image"]];
UIBarButtonItem *barButtonItem = [[UIBarButtonItem alloc] initWithCustomView:imageView];
You can scale down the image before setting it to the image view if you want to.

Parse Background image in loginView, not fitting to fit all iPhone device screen sizes

I am trying to get my background image to fit all screen sizes within Parse's logInView. Unfortunately this code isn't doing the trick I expect it to.
[self.logInView setBackgroundColor:[UIColor colorWithPatternImage:[UIImage imageNamed:#"Backgroundwithlogo.png"]]];
I have subclassed the PFLoginViewController to what I just called LoginViewController. I have tried resizing the image that i imported, but it either adjusts itself to be too big, or too small. The colorWithPatternImage will tile my image across the view if it's too small, and my background image seems to be stretched horizontally right now.
What I have tried:
I have tried implementing:
UIImageView *imgView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"Backgroundwithlogo.png"]];
[self.logInView addSubview:imgView];
[self.logInView sendSubviewToBack:imgView];
imgView.contentMode = UIViewContentModeScaleAspectFit;`
with no avail. I have read the Parse Guidelines Here. But they don't cover this issue with sizing problems inside the UI. Any help would be greatly appreciated.
UIImageView *imgView = [[UIImageView alloc] initWithFrame:[UIScreen mainScreen].bounds];
imgView.image = [UIImage imageNamed:#"Backgroundwithlogo.png"];
[self.logInView addSubview:imgView];
[self.logInView sendSubviewToBack:imgView];
Also, you may need a specific contentMode.

crush the height and width of the initWithFrame image size

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;

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.

Strange behaviour with UIImageView with camera pictures, all zoomed in

I have a strange behaviour which suggests I might just be missing something. I have the following code:
UIImage *anyImage = [UIImage imageWithContentsOfFile:path];
self.imageView = [[UIImageView alloc] initWithImage:newImage];
[self.imageView setImage:newImage];
self.imageView.center = self.view.center;
self.imageView.contentMode = UIViewContentModeScaleAspectFit;
self.imageView.userInteractionEnabled=YES;
self.imageView.clipsToBounds=YES;
[self.view addSubview:self.imageView];
When anyImage is a smaller size picture, downloaded from the internet, it displays correctly. However, when anyImage is a picture taken from my iPhone camera, it is zoomed-in and only shows a part of the picture enlarged, covering the entire window. I have tried different approaches to resize the imageView but it has no effect. So for example, a picture of a person might have the head or part of the body all zoomed in to cover the entire window and the rest of the body not shown. How can I resolve this problem?

Resources