Make a rectangle equal to image view - iphone

I have an imageView. I need to restrict the size of imageView to the white background imageOnly.The image is tightly bounded between right,left and top margin. I am confused on how to do at bottom, such that the image must fit in the white background only. How do I do that ?
http://i.stack.imgur.com/ztvQe.png
The code I am using
_imageView = [[UIImageView alloc] initWithFrame:CGRectMake(25, 240,175,205)];
Frame width and height is 205*205

You can add your imageView as a subview of the view with the white background.
UIView *whiteBackgroundView = [[UIView alloc] initWithFrame:CGRectMake(25,240,205,205)];
CGSize whiteSize = whiteBackgroundView.frame.size;
_imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0,size.width,size.height)];
[whiteBackgroundView addSubview:_imageView];
In the example your imageView will completely fit the size of the white background view.

Try this
UIView *productView = [[UIView alloc]initWithFrame:CGRectMake(25, 240,225,225)];
_imageView = [[UIImageView alloc] initWithFrame:CGRectMake(10, 10,205,205)];
_imageView.clipsToBounds =YES;
[productView addSubview:_imageView];
[self.view addSubview:ImageBackview];

just create one view and add this image in it, for example use bellow code..
UIView *ImageBackview = [[UIView alloc]initWithFrame:CGRectMake(25, 240,225,225)];
_imageView = [[UIImageView alloc] initWithFrame:CGRectMake(10, 10,205,205)];
[ImageBackview addSubview:_imageView];
[self.view addSubview:ImageBackview];

Related

How to set UIImageView frame in ViewController for Universal App?

For a Universal App, how do I set the UIImageView frame in the ViewController?
UIView *baseView = [[UIView alloc] init];
[self.view addSubview:baseView];
// Displays UIImageView
UIImageView *myImage = [[UIImageView alloc]
initWithImage:[UIImage imageNamed:#"tool.png"]];
[baseView addSubview:myImage];
When I run app it shows black screen. So i guess I have to set a frame for it.
You kinda answered your own question - yes you have to set a frame for it.
You may want to try:
UIView *baseView = [[UIView alloc] initWithFrame:self.view.bounds];
[self.view addSubview:baseView];
// Displays UIImageView
UIImageView *myImage = [[UIImageView alloc]
initWithImage:[UIImage imageNamed:#"tool.png"]];
myImage.frame = baseView.bounds;
[baseView addSubview:myImage];
You could also set the autoresizingmask for baseView and myImage so that as self.view's frame changes, so do these two views.
Also, just in case you aren't using ARC - don't forget to autorelease these views before leaving this method.

iOS - how to put UIImage and UILabel as a footer on the same line?

I need to have a footer in my view that says "Powered by: {image_logo_of_my_company}" How do I achieve that?
Make a wrapper view of type UIView and add a UILabel and the logo in a UIImageView inside that view at the appropriate coordinates.
UIView *wrapper = [[UIView alloc] initWithFrame:footerFrame];
UILabel *label = [[UILabel alloc] initWithFrame:
CGRectMake(0, 0, footerFrame.size.width*0.6, footerFrame.size.height)];
UIImageView *imageView = [[UIImageView alloc] initWithFrame:
CGRectMake(footerFrame.size.width*0.6, 0,
footerFrame.size.width*(1.0-0.6), footerFrame.size.height)];
[wrapper addSubView:label];
[wrapper addSubView:imageView];
label.text = #"Powered by: ";
imageView.image = [UIImage imageNamed:#"logo"]; // assumes logo.png & logo#2x.png
[viewNeedingFooter addSubView:wrapper];
Assuming you need 60% of the width of the footer for the label and 40% for the logo.

How to reset the background of a UIView

After adding a background (image) to my UIView (View_0) I wish to implement a button to remove the same background.
How can I do it? I have tried to set a different image as the background of View_0 but this does not work (I suspect this background is set behind "image").
I don't want to use
[View_0 removeFromSuperview];
as I need View_0 to still be there for other purposes....
//Create an ImageView
UIImageView *image = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 940, 422)];;
//Add the ImageView as the background of the View_0
[View_0 addSubview:image];
//Move custom image behind View
[View_0 sendSubviewToBack: image];
//Failed attempt to remove the background....
View_0.backgroundColor = [[UIColor alloc] initWithPatternImage:[UIImage imageNamed:#"defaultImage.PNG"]];
You have added a UIImageView to the View and Not the background of the view. So what is happening is that when u implement
View_0.backgroundColor = [[UIColor alloc] initWithPatternImage:[UIImage imageNamed:#"defaultImage.PNG"]];
The background of the View is being set but the UIImageView is now on top of the View so that is why the background of the View is not being shown.
Instead if you simply want to add the background you can change it to the Action of the UIButton.
Also if you use [View_0 removeFromSuperview]; you are trying to remove the view which is not right.
//Create an ImageView
UIImageView *image = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 940, 422)];;
image.tag = 1234;
//Add the ImageView as the background of the View_0
[View_0 addSubview:image];
//Move custom image behind View
[View_0 sendSubviewToBack: image];
UIImageView *imgView = [self.view viewWithTag:1234];
[img setImage:nil];
This should work i think

how to place a UIImageView in a particular area of a subView

I am woundering if there is a way of placing a UIImageView into a specific area of another view using addSubview
this is how my code currently looks, I am woundering how I would add the image to a particular spot of the view its being added to, say (0,0) top left.
this is what I am doing atm.
//...
jumpBar = [[JumpBarViewController alloc] initWithNibName:#"JumpBarViewController" bundle:[NSBundle mainBundle]];
jumpBarContainer = [[UIView alloc] initWithFrame:CGRectMake(0.0, 480.0, (jumpBarHeight + 49.0), 320.0)];
[jumpBarContainer addSubview:jumpBar.view];
[self.view insertSubview:jumpBarContainer belowSubview:actionTabBar];
// Add jumpBar shade
switch (jumpBarHeight) {
case 103:
{
NSLog(#"one row");
UIImageView *smlShader = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"1-row-top-bg.png"]];
[jumpBarContainer addSubview:smlShader]; // how do i add this to the top left of jumBarContainer?
}
//...
any help would be greatly appreciated :)
Set the UIImageView frame:
UIImageView *smlShader = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"1-row-top-bg.png"]];
smlShader.frame = CGRectMake(x,y,w,h); //Change the value of the frame
[jumpBarContainer addSubview:smlShader];

iPhone: Display image at its original size

I am trying to set frame for imageview to original size of image and display it in center of view. I have written following code for this but image still appears in whole screen! Suppose image size is 320x88, it appears resized/stretched to 320x460! I want it to appear in center of screen so I set Y coordinate to (460-88)/2 but it doesn't work. Could anyone please help me to resolve this. Thanks.
UIImageView *imageView = [[UIImageView alloc] initWithImage:[UIImage imageWithContentsOfFile:filePath]];
if(imageView.image.size.width == IPHONE4_RES_WIDTH)
[imageView setFrame: CGRectMake(0, 0, imageView.image.size.width/2, imageView.image.size.height/2)];
UIViewController *viewController = [[[UIViewController alloc] init] autorelease];
viewController.view.backgroundColor = [UIColor blackColor];
NSLog(#"%f : %f",viewController.view.bounds.size.height, imageView.bounds.size.height);
viewController.view = imageView;
viewController.view.contentMode = UIViewContentModeCenter;
[self.navigationController pushViewController:viewController animated:YES];
try this
viewController.view.contentMode=UIViewContentModeCenter;
you can set the uiimageview to not stretch the image.. even if it is full size it will show the image in the center when you set
imageView.contentMode = UIViewContentModeCenter
Ran across this question while trying to achieve the same in a storyboard file. Just as an addition: in storyboard for the same thing you can select Mode = Center of UIImageView
Following works perfectly for my requirements!
UIImageView *imageView = [[UIImageView alloc] initWithImage:[UIImage imageWithContentsOfFile:filePath]];
// Reduce the width and height for iPhone 4 res
if(imageView.image.size.width == IPHONE4_RES_WIDTH)
[imageView setFrame: CGRectMake(0, 0, imageView.image.size.width/2, imageView.image.size.height/2)];
UIViewController *viewController = [[[UIViewController alloc] init] autorelease];
viewController.view.backgroundColor = [UIColor blackColor];
float X = (viewController.view.bounds.size.width - imageView.bounds.size.width)/2;
float Y = (viewController.view.bounds.size.height - imageView.bounds.size.height)/2 - 50; // Size of tabbar and navigation bar deducted
NSLog(#"(%f, %f) : (%f,%f)",X,Y,imageView.bounds.size.width,imageView.bounds.size.height);
[viewController.view addSubview:imageView];
//viewController.view = imageView;
//viewController.view.contentMode = UIViewContentModeCenter;
// Set in the center of the screen
if(imageView.image.size.width == IPHONE4_RES_WIDTH)
[imageView setFrame: CGRectMake(X, Y, imageView.image.size.width/2, imageView.image.size.height/2)];
else
[imageView setFrame: CGRectMake(X, Y, imageView.image.size.width, imageView.image.size.height)];
[self.navigationController pushViewController:viewController animated:YES];