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

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.

Related

How to set a small image on a uiimage view on a particular position?

I created an UIView.
I need to set a small image on a particular position.
I can do it setting the image in a small separate view.
But, I plan to do it dynamically on a Large UIView which is full screen.
Thanks.
UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0,0,320,200)];
view.backgroundColor=[UIColor clearColor];
[self.view addSubview:view];
// add image in view
UIImageView *imageView =[[UIImageView alloc] initWithFrame:CGRectMake(50,50,20,20)];
imageView.image=[UIImage imageNamed:#"image.png"];
[view addSubview:imageView];
That is what views are for: manipulating UI widgets.
By far, your best approach is to position a UIImageView.
You can also override the view drawing methods:
override func drawRect(rect: CGRect) {
// Curstom
}
Try this code.
UIImage *image = [UIImage imageNamed:#"imagename.png"];
UIImageView *imageView = [[UIImageView alloc] initWithImage:image];
//specify the frame of the imageView in the superview , here it will fill the superview
imageView.frame = catView.bounds;
// set custom frame as per your requirement
//or
// imageView.frame = CGRectMake(10,10,30,30);
//or
//according to superviews frame
//imageView.frame=CGRectMake(10,10,yourUIViewObject.bounds.size.width/2,yourUIViewObject.bounds.size.height/2);
// add the imageview to the superview
[yourUIViewObject addSubview:imageView];

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 remove the UIImageView From the custom UIView

i create the the imageView by programatically and add that imageView to the Custom UIView.then i by click the button i want to add another imageView to the same custom UIView,here the old and new imageView also displaying,so i need to remove the first added imageView then i add the new imageView. help me to remove the imageView..
CGRect rect=CGRectMake(10, 10, 100,100);
UIImageView *imageView=[[UIImageView alloc]init];
imageView.frame=rect;
imageView.backgroundColor=[UIColor redColor];
[self.customView addSubview:imageView];//add the imageView to UIView
When you add the first imageView set its tag like
imageView.tag = 232;
Later you can ask your customView for this imageView and remove it.
Call this function.
[[customView viewWithTag:232] removeFromSuperview];
Use this code.
[[self.customView viewWithTag:1000] removeFromSuperView]
CGRect rect=CGRectMake(10, 10, 100,100);
UIImageView *imageView=[[UIImageView alloc]init];
[imageView setTag:1000];
imageView.frame=rect;
imageView.backgroundColor=[UIColor redColor];
[self.customView addSubview:imageView];

UIImageView shows outside its parents frame

I'm developing a iOs app for iPad and I'm implementing a UIImageView inside a UIView. The problem is that if the position of the image view has a position out of the UIView that it is inside, you can see it. The code:
UIView *vistafunda = [[UIView alloc] initWithFrame:CGRectMake(512/2, 48, 525, 651)];
[self.view addSubview:vistafunda];
vistafunda.backgroundColor = [UIColor darkGrayColor];
UIImageView *img = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"hola.png"]];
img.center = CGPointMake(12, 200);
[vistafunda addSubview:img];
I would like that the image view was always under the view, so if the image is outside the view you could not see it.
Thanks!
Just make the containing view clip it's children
vistafunda.clipsToBounds = YES;

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