My current problem is that when i try rotating a UIImageView it rotates but it resizes too.
Here's the code i'm using:
CGAffineTransform a = _player.image.transform;
_player.image.transform = CGAffineTransformRotate(a, angle);
_player is a Player object and image is a Player UIImageView variable.
Thanks for the tip.
EDIT:
pic 1: Application launched
enter link description here
i can move a player by touching it and using the pad
pic 2: i want to move the car and rotate it as the pad but the car resizes and doesn't move.
enter link description here
Any ideas?
set autoResizingMask of the view property to UIViewAutoresizingMaskNone
Try turn off "Autoresize Subviews" attribute (in the XIB file) for superview of the UIImageView. You can found this answer here too: UIView resizing but not the SubViews .
Related
I have a UIImageView and when I set an image in that view using IB, the image is the correct size when I run the app (40x40). When I programmatically change the image using the code below, the new image is 400x400 or so. This is using the same image that I know works fine if it's set in IB. Do I have to scale the image before I add it to the UIImageView? I assumed auto-layout's constraints would automatically do this for me.
UIImage *messageTypeImage = [UIImage imageNamed:message.imageName];
[messageCell.imageView setImage:messageTypeImage];
[messageCell.contentView layoutIfNeeded];
The UIImageView's content mode is set to "Scale To Fill" and I've tried adding the code below both before and after calling setImage:
[messageCell.imageView setContentMode:UIViewContentModeScaleAspectFill];
[messageCell.imageView setClipsToBounds:YES];
Here's how the constraints are set up for the image view:
Are you using iOS 8 SDK? If so, try overriding layoutSubviews in your cell subclass and add this:
self.contentView.frame = self.bounds;
self.contentView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
There are some problems with the contentViews' of collection view/table view cells constraints/masks.
Wow. Finally found the answer here. Turns out, there's a default imageView outlet in prototype cells. I was unknowingly using that outlet and not the UIImageView I thought I was using.
I have made a subclass for UITableViewCell and I am implementing Subtitle TableViewCell with a thumbnail image.
Following are the UITableViewCell contents:
The issue I am facing is when the data loads in TableViewCell, the subtitleLabel text gets hidden upto the height of the imageView. But when I select any Cell, it shows subtitleLabelText completely.
I have added the screenshot of the same for complete reference:
The UIImageView has frame = CGRectMake(0,0,40,40);
Try to give a clearColor background color for the cell title label -
cell.textLabel.backgroundColor = [UIColor clearColor];
It turns out I was using TableViewCell style as subtitle instead of custom. The style settings in subtitle was making the other labels to hide below them. What a silly miss!
In your nib or storyboard file, make sure that the label is below the image view in the list of subview components (it is in the left of the screen). The first subview in that list will be at the lowest level (behind every other subview, if they overlap).
write one line of code
Titlelabel.backgroundcolor = [UIColor ClearColor];
because your label has white background..and Titlelabel height is too large so label is colliding.
Let me know working or not!!!
Happy Coding!!!
What is the frame of Title Label? if its height is more, then also it may possible that it hides your subtitle Label
Here's a great tutorial which helped me when I was trying to do something like you want :
http://www.appcoda.com/ios-programming-customize-uitableview-storyboard/
You can adapt the size of the different components (ImageView, TitleLabel, Subtitle,...)
I want image coordinates in Uiimageview not UIImageview coordinates.
Because if i have a landscape mode image and i set the property of UIImageview contentmode aspectfit, It will show in portrait mode with white spaces and centered.
Please tell me how to get the x,y coordinate of images in UIImageView.
Thanks
UIImage does not have a frame,Frame is for UIImageview
Try to create a UIImage from your image data or file, then use the size property to get a CGSize structure representing the image's dimensions. See Apple reference doc.
Hope this helps you!
I have an Image inside UIScrollView which i can zoom in and out.
I have a button that let the user rotate the Image 90 degrees:
(void)RotateImage {
CGAffineTransform rotateTrans = CGAffineTransformMakeRotation(-90.0 / 180.0 * 3.14);
BaseImg.transform = rotateTrans;
}
After the imaged is rotated i cannot zoom in and out.. the image is going crazy on the screen and going back to the UNRotated state.
What am i doing wrong? code examples will be great!
Thanks :)
UIScrollView likes to take over the transforms of the views it contains. There are two solutions:
Rotate the image without changing the containing view.
Create a UIView subclass that displays an image within a sublayer.
To rotate the image, see How to Rotate a UIImage 90 degrees?. If you're always and only doing 90 degree rotation, see #Peter Sarnowski's solution. To adapt it to what you're doing here, assuming that BaseImg is a UIImageView:
- (void) rotateImage
{
UIImage *sourceImage = [baseImg image];
UIImage *rotatedImage = [UIImage imageWithCGImage:[sourceImage CGImage] scale:1.0 orientation:UIImageOrientationRight];
[baseImg setImage:rotatedImage];
}
This will only rotate once. To have rotateImage work repeatedly, read the existing orientation property and move it on to the next in clockwise or anticlockwise order.
If the image is not square, you may also need to resize baseImg to reflect its new aspect ratio.
To create a UIView subclass, you need to have it store a CALayer as a sublayer of the view layer. Store the image in the sublayer, and transform the sublayer at will. This is faster, and allows arbitrary rotation, but you need to calculate your own scaling to prevent the rotate image going outside the view bounds.
To simply rotate image the code is perfect but to add zoom in and out and then add rotation you need to retain its transforms.Here is a sample code that can help you.
https://github.com/elc/iCodeBlogDemoPhotoBoard
I have a UIImageView as a subview of UIView (frame size 300 x 300). My UIImage would be either portrait, landscape or some odd sizes. How do I fill in the UIImage within the UIView frame size without stretching the image to 300 x 300?
The purpose of this, I will be adding another UIImageView as the image border.
Please help.
Thanks.
Try
[img setContentMode:UIViewContentModeScaleAspectFit];
UIViewContentModeScaleAspectFit: Scales the content to fit the size of the view by
maintaining the aspect ratio. Any remaining area of the view’s bounds is transparent.
or [img setContentMode:UIViewContentModeScaleAspectFill];
UIViewContentModeScaleAspectFill: Scales the content to fill the size of the view. Some
portion of the content may be clipped to fill the view’s bounds.
if you are clipping subviews, then you need to do
[imgView clipToBounds:YES];
Ah I just reread what you were asking, I know what your question is now.
Your UIImageView's frame changes, and when it does so does your image. You don't want your image to change, but you do want your ImageView to adjust to fill the view it is contained in.
UPDATE
I figured it out.
[self.imageView setContentMode:UIViewContentModeCenter];
No matter what orientation, the size stays the same.
You also have the option of setting it to align top, bottom, left, right, top left, top right, bottom left, bottom right, all of which only help to align your image and NOT redraw or re-size the image.
Does that help?
[image setContentMode:UIViewContentModeScaleToFill];
Set the contentMode property of UIImageView to either of this values depending on where you want to put it in the superview:
UIViewContentModeCenter,
UIViewContentModeTop,
UIViewContentModeBottom,
UIViewContentModeLeft,
UIViewContentModeRight,
UIViewContentModeTopLeft,
UIViewContentModeTopRight,
UIViewContentModeBottomLeft,
UIViewContentModeBottomRight,
As this values:
UIViewContentModeScaleToFill,
UIViewContentModeScaleAspectFit,
UIViewContentModeScaleAspectFill,
Will indeed cause the image to be 300 x 300, which is what you don't want.
Use the contentMode property of UIImageView.
Edit: and set the image size to 300x300.