How can I change the height of an image in Xcode? - objective-c

I am trying to adjust the height of an image by stretching it.
This is what I've got so far:
-(IBAction)buttonTapped{
UIImage *img = [UIImage imageNamed:#"water.png"];
[water1 setImage:img];
}
-(IBAction)button2Tapped
+ (UIImage*)imageWithImage:(UIImage*)img
scaledToSize:(CGSize)newSize;
{
UIGraphicsBeginImageContext( newSize );
[img drawInRect:CGRectMake(0,2,newSize.width,newSize.height)];
UIImage* newImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return newImage;}
and this is in my view controller.h
IBOutlet UIImageView *water1;
-(IBAction)buttonTapped;
-(IBAction)button2Tapped;
I am not sure what I need to change to make this correct, or if I should start again with a new way.
I want it so that when I push a button, an image appears, then it resizes it when I push another button.
Thanks for helping!

You can simply change the size of the UIImageView property, in order to get the size you want.
Set the contentMode of the UIImageView to wathever fits your need, and it will automatically resize to the given size (either scaling to fit or stretching).

Related

Get Image from image View

if we add more than one View to imageView and NSString also. Is it possible to get image from ImageView that contain all images and strings etc?
like this
From what I understand in your question, you have added an ImageView for the building, another for the video symbol (as a subview) and a UILabel (again as a subview) for the time. You want all these to be rendered as a single UIImage. For this, you can use:
UIGraphicsBeginImageContext(imageView.frame.size);
CGContextRef context = UIGraphicsGetCurrentContext();
[imageView.layer renderInContext:context];
UIImage *screenShot = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
The screenshot UIImage will contain your desired image.

Image aspect ratio change after inserting it into imageview

So I'm trying to load an image from camera into image view. I've got it done, but images aspect ratio changes.
I tried setting content mode for imageview but no effect.
Here is how i do it:
UIImage *picture=[UIImage imageWithData:slika];
imageview = UIViewContentModeScaleAspectFit;
[imageview setImage:picture forState:UIControlStateNormal];
Any ideas?
Is image view your own view? Or it's, for example, cell's default imageView?
And you should write so:
imageView.contentMode = UIViewContentModeScaleAspectFit;
imageView.image = [UIImage imageNamed:picture];
or
[imageView setImage:picture];
There is no method [imageview setImage:picture forState:UIControlStateNormal]; for UIImageView, it only available for UIButton (and may be for some other UIControls, but UIImageView not subclass of UIControl - it direct subclass of UIView) . But button does not scale images preserving aspect ratio.

set UIImageView to display middle section of an image

I've got an image view that I want to show a central slice of the user's profile picture. I want to scale the picture so that the width matches the width of the screen while maintaining the aspect ratio, center the image, and then have any extra on the top/bottom to not appear at all.
I've tried setting the imageView.contentMode to UIViewContentModeScaleAspectFill, UIViewContentModeScaleAspectFit, UIViewContentModeCenter and a few others, but it either resizes my UIImageView, changes the aspect ratio, or doesn't fill the area.
"but it either resizes my UIImageView"
I guess you see the cropped parts.
Maybe you forgot the "clipsToBounds" property?
UIImageView *view1 = [[UIImageView alloc] initWithFrame: ___ ];
view1.clipsToBounds = YES;
UIImage * img = [UIImage imageNamed:#" ___ "];
view1.image = img;
view1.contentMode = UIViewContentModeScaleAspectFill;

Show portion of UIImage in UIImageVIew

This question might have been answered, but I can't find a solution to my problem after hours of googling.
I have a UIImageView (320*320) in a custom UITableCell with a UIImage (320*320) inside.
When I click on a button, the UIImageView resizes to 320*80 (animated) and the UIImage too.
How could I do to prevent the UIImage from resizing ?
I would like this UIImage to stay but same and show only a portion of this image ( a 320*80 frame with 0/0 origin)
Edit : Cropping
I thought of cropping the image to the right size and then set the UIImageView's image to the fullsize image, but it looks ugly because when the user clicks on the UIImageView, this one is still 320*320 but the UIImage has been set to the cropped one (320*80) so it is streched...
Thanks
I'm doing something similar in one of my apps, when I create the UIImageView object, I set the property contentMode to UIViewContentModeScaleAspectFill, and the clipsToBounds property to YES. Hope this helps.
You can resize the image and then replace the UIImageView image with the resized one.
CGImageRef imageToBeResized = yourImage.CGImage;
CGImageRef partOfImageAsCG = CGImageCreateWithImageInRect(imageToBeResized, CGRectMake(0, 0, 320, 80));
CGRelease(imageToBeResized);
UIImage *resizedImage = [UIImage imageWithCGImage:imageToBeResized];
Maybe you should think about use a UIScrollView with your UIImageView inside. So, you can with its properties to archive the desired behaviour.

Does anyone know how I can get the "real" size of the UIImageView instead of creating a frame

I was hoping there was some sort of function that could take the real size of my picture
CGRect myImageRect = CGRectMake(0.0f, 0.0f, 320.0, 210.0f); // 234
UIImageView *myImage = [[UIImageView alloc] initWithFrame:myImageRect];
[myImage setImage:[UIImage imageNamed:#"start.png"]];
And with CGRectMake which does something like this ..( imageViewHeight ,
CGFloat imageViewHeight = [myImage frame].size.height;
So that I could get the real size instead of having to define it like you can seen above.
I don't really get what you're asking, but here's a shot:
If you have a UIImage, and you want to know its size, you can ask it for its -[UIImage size] property.
However, if you want to create a UIImageView that's the same size as a UIImage, then you can just use -[UIImageView initWithImage:], which will automatically set the frame of the UIImageView to correspond to the dimensions of the image.
If, however, you're just looking to change the dimensions of a currently existing view, there's really no easy way to do that without messing around with the view's frame. You could maybe apply an affine transform to scale it, but it's easier to manipulate the frame.
It looks like you're asking to add the image to the imageview without first creating a frame. If this is the case, you can do the following:
UIImageView *myImage = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"start.png"]];
As far as I understood, you are looking for the size of the image used in UIImageView object. To do that, there is not a function built in UIImageView but you can do it this way:
NSString* image= [myImage image].accessibilityIdentifier; // Get the image's name
UIImage *img = [UIImage imageNamed:image]; // Create an image object with that name
CGSize size = img.size; // Get the size of image
Hope this helps your question.