Unable to center a UIImage in a UIScrollView - ios

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.

Related

ImageView with same size of View(full screen) - Objective C

How to do programmatically ImageView with same size of View in a way that there is no difference between devices ?
UIImageView *imgV =[[UIImageView alloc] initWithFrame:self.view.frame];
imgV.image=[UIImage imageNamed:#"draw.png"];
[self.view addSubview:imgV];
suppose draw.png is xx size then draw#2x = 2(xx), draw#x = 3(x*x)
please supply image draw.png, draw#2x.png , draw#3x.png in resource
hope it help
With code you can create a UIImageView with giving the frame of superview.
UIImageView* myImageView = [[UIImageView alloc]initWithFrame:self.view.frame];
[myImageView setImage:[UIImage imageNamed:#"yourImage.jpg"]];
[self.view addSubview:myImageView];
By Autolayout using storyboards we can do like this.
Drag an UIImageView to the view and provide these constraints and add.

Add image to view (background)

I have added an image to my viewcontroller in code. Without the use of an imageview. This is how i did it:
UIImageView *backgroundImage =[[UIImageView alloc] initWithFrame:CGRectMake(50,50,100,500)];
backgroundImage.image=[UIImage imageNamed:#"backgroundImage"];
[self.view addSubview:backgroundImage];
There are 2 things i need a litte help with. How can i get the image to the back of the view, behind the labels, buttons etc.
And how do i get the image to fit all iphone screen sizes?
I hope you can help me.
Much thanks
As you asked two questions-
Question 1 : How to get the labels/ buttons other subviews visible over the image?
Answer -
Just add image before any other subview.
And there are two ways to set a background image to a UIView –
You can set your view background color to color created with UIColor’s colorWithPaternImage.
You can add a UIImageView subview to your view.
colorWithPaternImage was created to use small images to create a pattern image that will be repeated. Using it with large images wont be a wise decision. So if you want to have a patterned image background with uses small images which will be repeated to fill the background, then you should use colorWithPaternImage, as it will do it quickly and wont consume much memory.
self.view.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:#"background"]];
If you have a full size background image, then you should definitely use the UIImageView. Using UIImageView will save a lot of memory in this case.
UIImageView *backgroundView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"background"]];
[self.view addSubview:backgroundView];
Question 2 : How to show the image as per the device/ screen resolution?
Answer -
CGRect screenBounds = [[UIScreen mainScreen] bounds];
Use the above screenBounds to set the frame of ImageView.
self.view.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:#"backgroundImage"]];
Just replace your code with this :
UIImageView *backgroundImage = [[UIImageView alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
backgroundImage.image = [UIImage imageNamed:#"backgroundImage"];
[self.view insertView:backgroundImage atIndex:0];
Hope this helps. Let me know it.. :)
Simply get screen size of device by following method to make it compatible to all device.
CGRect rect = [UIScreen mainScreen].bounds;
create UIImageView object of that frame size and insert at index 0 of UIView.
UIImageView *backgroundImage =[[UIImageView alloc] initWithFrame:rect]; // This will set image fit to all iphone...
backgroundImage.image=[UIImage imageNamed:#"your_image.png"];
[self.view insertSubview:backgroundImage atIndex:0];
UIImageView *backgroundImage =[[UIImageView alloc] initWithFrame:self.view.bounds]; // This will set image fit to all iphone...
backgroundImage.image=[UIImage imageNamed:#"your_image.png"];
[self.view addSubview:backgroundImage];
// This line will send image behind all the other controls
[backgroundImage sendSubviewToBack:self.view];

How to keep a stronger reference of a UIImageView?

the problem is that the image i want to show fist stays in the middle of the screen during the entire scrolling proccess obscuring the rest of the views contents. Any fixes ? My storyboard had a UIScrollView > list view and over it a UIImage connected to IBOutlets. Here is the rest of the code. Thanks.
UIImageView * imageView = [[UIImageView alloc]init];
[imageView setImage:[UIImage imageNamed:#"pcat3.png"]];
[imageView setFrame:CGRectMake(10,10,300,300)];
[imageView setContentMode:UIViewContentModeScaleToFill];
[imageView setUserInteractionEnabled:TRUE];
[self->listView addSubview:imageView];

Rotated imageview not appearing after addsubview

I have added an arrow image view and I want to rotate it to create a corner.
My code is pretty simple:
[self.view addSubview:self.upLeftCorner];
self.upLeftCorner.transform = CGAffineTransformMakeRotation(2.35619449);
The imageview appears when i don't rotate it, however it does not after the rotation. I tried adding the subview after the rotation but still it does not appear. Any help?
Well, this is working:
UIImage *image = [UIImage imageNamed:#"image.png"];
UIImageView *imageView = [[UIImageView alloc] initWithImage:image];
[imageView setFrame:CGRectMake(0, 0, 100, 100)];
imageView.transform = CGAffineTransformMakeRotation(2.35619449);
[self.view addSubview:imageView];
I think its because you did the addSubview before rotating the imageView. In my code, I rotated then addSubview

ios - SizeToFit on UIImageView not working

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?

Resources