ios - SizeToFit on UIImageView not working - ios

I have a UIImageView which loads in images from the documents directory on the ios file system .
The problem is that when I call
[imageView sizeToFit];
It does not work. I think it is because the image hasn't loaded fully at that point and so the sizeToFit part is called before it has the image width & height.
Could someone point me in the direction of a fix please.
Here is my code:
imageView.contentMode = UIViewContentModeScaleAspectFit;
imageView.image = [UIImage imageWithContentsOfFile:[FileOperations getLibraryPath:[NSString stringWithFormat:#"%d.png", listData.imageId]]];
[imageView sizeToFit];
[imageScroll setContentOffset:CGPointMake(0, 0)];
imageScroll.contentSize = imageView.frame.size;
imageScroll.decelerationRate = UIScrollViewDecelerationRateFast;
imageScroll.minimumZoomScale = 1;
imageScroll.maximumZoomScale = 2;
[imageScroll setZoomScale:imageScroll.minimumZoomScale];
[imageScroll addSubview:imageView];
Thanks,
Ashley

Check the image size:
NSLog(#"%#", NSStringFromCGSize(imageView.image.size));
You may want to set imageView frame based on the image size manually, for example, if the image is too large to fit the imageView's superview.

UIImageView *myImage = [[UIImageView alloc]init];
myImage.contentMode = UIViewContentModeScaleAspectFit;
myImage.image = [UIImage imageName:#"image.png"];
[self.view addSubview:myImage];

sizeToFit should work in your situation, but fails if the UIImage is nil.
UIImage's imageWithContentsOfFile: returns a (completely loaded) reference to a UIImage or nil. Are you sure that you're passing the right path?

Related

Objective c: How to set the UIImageView image

hai iam new one for objective c and iOS development
Set the UIImageview image. try the following code
arrowImgVw is UIImageView Class object
UIImage *image=[[UIImage alloc]init];
image=[UIImage imageNamed:#"return_journey_denoter.png"];
arrowImgVw.image = image;
hey you are wrong in some point use this code for adding an image into UIImageView and display it on iPhone devide
UIImageView *myImage = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 320, 460)];
//Allocating imageView and set its frame.
myImage.image = [UIImage imageNamed:#"return_journey_denoter.png"];
//add image into imageview
[self.view addSubview:myImage];
// add imageview into your viewcontroller's view
To simply set image to imageView just one line of code
arrowImgVw.image = [UIImage imageNamed:#"return_journey_denoter.png"];
The code you have written is ok for setting image, just check the name for the given image or you can try following code...
UIIImageView *arrowImgVw = [[UIImageView alloc] initWithFrame:CGRectMake(0,0,200,200)]; //just example change the frame as per your need
arrowImgVw.image = [UIImage imageNamed:#"return_journey_denoter"];
If you are trying to access the image from the assets . Use the below code
NSBundle *bundle = [NSBundle bundleForClass:[self class]];
UIImage *image = [UIImage imageNamed:#"image" inBundle:bundle compatibleWithTraitCollection:nil];
UIImageView *imageView = [[UIImageView alloc] initWithImage:image];
image should be inside your assets folder .
In Swift3.0
To set image to your imageView use belwo code.
arrowImgVw.image = UIImage(named: "return_journey_denoter.png")

How to display image in iphone simulator using objective C

I am trying to display image in iphone simulator programmatically using objective c, I am using following code, but it is not displaying anything in simulator
imageview = [[UIImageView alloc] init];
UIImage *myimg = [UIImage imageNamed:#"A1.jpg"];
imageview.image=myimg;
You can use this code
imageview = [[UIImageView alloc] init];
UIImage *myimg = [UIImage imageNamed:#"A1.jpg"];
imageview.image=myimg;
imageview.frame = CGRectMake(50, 50, 150, 150); // pass your frame here
[self.view addSubview:imageview];
Maybe you forgot this:
imageView.frame = self.view.bounds;
[self.view addSubview:imageview];
If you are programmatically creating the imageView then you must add it to the view heirarchy using below line
[self.view addSubview:imageview];
Add your image view into view like this
imageview = [[UIImageView alloc] initWithframe:CGRectMake(0, 0, 100, 200)];
UIImage *myimg = [UIImage imageNamed:#"A1.jpg"];
imageview.image=myimg;
[self.view addSubview:imageview];
First you need to initialize your UIImageView and assign UIImage to it's image property. You already did it in your code. Then you can set the frame to image view.
imageview.frame = CGRectMake(50, 50, 150, 150);
But in most cases it will be more useful to pass sizeToFit message to your image view.
[imageview sizeToFit];
So it will change it's frame to fit image's size. But using this approach you need to assign the origin to your image view's frame later. So the best solution is assign the frame in initialization and then pass sizeToFit.
UIImageView *imageview = [[UIImageView alloc] initWithFrame:CGRectMake(10.0, 20.0, 0.0, 0.0)];
UIImage *myimg = [UIImage imageNamed:#"A1.jpg"];
imageview.image=myimg;
[imageview sizeToFit];
And don't forget to add image view to view hierarchy.
[self.view addSubview:imageview];
Also you need to check if your image is correctly added to your project. You can check it by finding you image file in Project Navigator and checking it's Target Membership in File Inspector. There should be a tick beside you target's name.

iOS: opening image from photolibrary

I'm calling a view (that contains just an ImageView) passing the picture's path from library's user. And then, on viewdidload I'm loading the image. But, the image is getting a "super zoom" on the upper right corner of the picture, I don't know why!
- (void)viewDidLoad
{
[super viewDidLoad];
self.navigationController.navigationBar.translucent = NO;
UIImage *picture = [UIImage imageWithContentsOfFile: self.picturePath];
UIImageView* imageView = [[UIImageView alloc] initWithImage:picture];
imageView.contentMode = UIViewContentModeScaleAspectFit;
[self.view addSubview:imageView];
}
Based on your comment, the image you are loading is very large. The UIImageView that you're creating is taking on the width/height of the image. You can either set the frame of imageView, or use and IBOutlet from your nib to add the photo to an existing UIImageView.
In your code you're creating a new UIImageView, so your updated code would look like:
- (void)viewDidLoad
{
[super viewDidLoad];
self.navigationController.navigationBar.translucent = NO;
UIImage *picture = [UIImage imageWithContentsOfFile: self.picturePath];
UIImageView* imageView = [[UIImageView alloc] initWithImage:picture];
// Set your frame size
imageView.frame = CGRectMake(0.0, 0.0, 320.0, 320.0);
imageView.contentMode = UIViewContentModeScaleAspectFit;
[self.view addSubview:imageView];
}

Centering a UIImageView inside a UIScrollView

I am trying to center a UIImageView in a scroll view. Here is the code I tried with no results.
UIImageView *iv = [[UIImageView alloc] initWithImage:[UIImage imageNamed:kReceiptDetailScrollViewBackgroundCornered]];
[iv setContentMode:UIViewContentModeCenter];
[[self scrollView] insertSubview:iv atIndex:0];
Anybody know what's wrong with this?
You set the image view's content mode to center, which means that the image is not scaled and it can be larger than the image view that displays it.
UIImage *image = [UIImage imageNamed: kReceiptDetailScrollViewBackgroundCornered];
UIImageView *iv = [[UIImageView alloc] initWithImage: image];
iv.contentMode = UIViewContentModeScaleAspectFit;
iv.frame = [self scrollView].bounds;
[[self scrollView] addSubview: iv];

Unable to center a UIImage in a UIScrollView

I have the following code in ViewDidLoad of my UiTableViewController:
UIImage *noImage = [UIImage imageWithContentsOfFile:pathOfNoImageFile];
UIImageView *imageview = [[UIImageView alloc] initWithImage:noImage];
[imageview setContentMode:UIViewContentModeScaleAspectFit];
imageview.frame = workingFrame;
[self.scrollview addSubview:imageview];
workingFrame.origin.x = workingFrame.origin.x + workingFrame.size.width;
[self.scrollview setPagingEnabled:YES];
[self.scrollview setContentSize:
CGSizeMake(workingFrame.origin.x,workingFrame.size.height)];
I therefore am only able to see the image partially and have attached a screen shot.
What can i do to fix it? I have Googled and looked at several Stack Overflow questions but have not been able to have it work. Please suggest. I have a scrollview in the uitableviewcell. I need to center my image vertically .
Your scrollview/cell size will need to be big enough to hold the imageview that you want to display. Looks like one or both is too small so I would adjust your sizes accordingly.

Resources