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

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.

Related

Set order on displaying paths/images

I have a couple of arcs and rects in a UIView, and a UIImageView as well. I want to have the UIImageView in the background. I have ordered it in IB so that the image view is behind my labels and buttons.
First, I need to put an image in the image view, which I don't want to do in IB. I've tried this code
UIImage *myImage = [UIImage imageNamed:#"image.png"];
UIImageView *myImageView = [[UIImageView alloc] initWithImage:#"myImage"];
But it doesn't work. After I get that working, what can I do to move my paths in front of the image view?
I think you want to draw to put a background image and on top of it want to draw circles(arcs).
If my understanding is correct. First draw the image on to the view using CGContextDrawImage() and then draw arcs on top of it. (No need to use image view).
Add image to UIImageView :
UIImage *myImage = [UIImage imageNamed:#"image.png"];
UIImageView *myImageView = [[UIImageView alloc] initWithImage:myImage];
[self.view addSubview:myImageView];

using UIImageView to show correct image size

how do you use UiimageView to show an image to make sure the imageview is the correct size of the UIImage? thanks!
This code should create an imageView with the frame of your image automatically.
UIImageView *imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"ImageName"]];
A little background from the docs.
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.

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;

Down-scaling an UIImage

I am having problems with resizing of my JPEG picture.
I would like it to be the size of the original picture, but instead it covers up the whole screen. I was trying myImageView = [[UIImageView alloc] initWithImage:[[UIImage imageNamed:#"Cube Tile.jpeg"]
myImageView.contentMode = UIViewContentModeScaleAspectFit;
This is what happened:
My simulator screen gets covered with my image, but the height is slightly smaller.
I also tried adding this: myImageView = [[UIImageView alloc] initWithImage:[[UIImage imageNamed:#"Cube Tile.jpeg"] resizableImageWithCapInsets:UIEdgeInsetsMake(0, 0, 0, 0)]];
This is what happened:
My simulator screen gets covered with tiny versions of my image.
Is it possible doing this without too many lines of code?
Excuse me if I am a bit vaque.
Note 1: The original picture is 24 x 24 pixels
Note 2: I am a new developer, so I was just experimenting.
Thanks in advance, Marnix.
Have you tried setting the frame on the image view? e.g.,
float x = 0.0;
float y = 0.0;
[myImageView setFrame:CGRectMake(x,y,24,24)];
(and to make sure it's not trying to autosize based on a setting in the .xib)
[myImageView setAutoresizingMask:UIViewAutoresizingNone];
You could change the value of
myImageView.contentMode = UIViewContentModeScaleAspectFit;
to one of these Values:
typedef enum {
UIViewContentModeScaleToFill,
UIViewContentModeScaleAspectFit,
UIViewContentModeScaleAspectFill,
UIViewContentModeRedraw,
UIViewContentModeCenter,
UIViewContentModeTop,
UIViewContentModeBottom,
UIViewContentModeLeft,
UIViewContentModeRight,
UIViewContentModeTopLeft,
UIViewContentModeTopRight,
UIViewContentModeBottomLeft,
UIViewContentModeBottomRight,
} UIViewContentMode;
See more at the official documentation http://developer.apple.com/library/ios/#documentation/uikit/reference/uiview_class/uiview/uiview.html

Overlay an UIImage above an UIImageView, possible? example code?

Is it possible to set an image over an other image,
respectively to overlay an image above an UIImage.
I give some code:
Headerfile:
#property (nonatomic,assign)UIImageView* imageView;
Implementationfile
UIImage *overlayImage = [UIImage imageNamed:#"lamp.png"];
The "imageView.image" has got an image and above that I will have this lamp, but later on it has to be possible to move it.
I searched some methods, but all methods gave me warnings.
Could you help me how to manage it? And is Core Animation an alternative to handle that?
Just add it as a subview. UIImageView is a UIView like any other.
UIImage *overlayImage = [UIImage imageNamed:#"lamp.png"];
UIImageView *overlayImageView = [[UIImageView alloc] initWithImage:overlayImage];
[self.imageView addSubview:overlayImageView];
You should use another UIImageView. A UIImage doesn't refer to an actual view in the interface, it's used as a reference to an image to be reused in code.
UIImageView* lampImageView= [[UIImageView alloc] initWithImage:overlayImage];
This way you can move it around anywhere over the first image view.