iPhone: Display image at its original size - iphone

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];

Related

Why is my attempt to display an image over the background causing a white display?

I have a background coming from Default-568h#2x.png. I want to display a shrunk PNG over part of the background, but calling:
[[CJSHCardView alloc] initWithScale:.1];
turns the display white. This does not happen if I comment this line, but in the function call, even if I make it return immediately it turns the display white after half a second:
- (id)initWithScale:(float)scale
{
UIImage *img = [UIImage imageNamed:#"note.png"];
self = [super initWithImage:img];
if (self != nil)
{
self.frame = CGRectMake(0, 0, img.size.width * scale, img.size.height * scale);
UIImageView *myImage = [[UIImageView alloc] initWithFrame:self.frame];
myImage.opaque = NO;
[self addSubview:myImage];
}
return self;
}
Moving/copying the return statement to be the first in initWithScale makes no discernible change from how it is with the return statement properly at the end: a brief view of the background, followed by a white screen. note.png is in "Supporting Files".
Do you see any gotchas where I can change things so that a shrunken version of the note (maintaining aspect ratio) displays? The note.jpg image does not have a pixel of white.
Thanks,
--EDIT--
Regarding the first comment:
#implementation CJSHCardView : UIImageView
- (id)initWithFrame:(CGRect)frame
{
NSAssert(NO, #"Call initWithHeight instead.");
return self;
}
Does that answer your question?
--SECOND EDIT--
Thank you for your response and your code, Nick. I have slightly altered it to fit ARC and to initially set a fixed-width scale, but I was a little unsure of what method to put it in. I tried the ViewController's viewDidLoad() method, but that resulted in the background being shown without hide or hair of the note. My present viewDidLoad() method reads:
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
UIImage *backgroundImage = [UIImage imageNamed:#"Default-568h"];
CGRect containerRect = CGRectZero;
containerRect.size = [backgroundImage size];
UIView *containerView = [[UIView alloc] initWithFrame:containerRect];
float scale=.1;
UIImageView *backgroundView = [[UIImageView alloc] initWithImage:backgroundImage];
[containerView addSubview:backgroundView];
[backgroundView sizeToFit];
// [backgroundView release];
UIImage *noteImage = [UIImage imageNamed:#"note"];
CGSize noteSize = [noteImage size];
UIImageView *noteView = [[UIImageView alloc] initWithImage:noteImage];
[noteView setContentMode:UIViewContentModeScaleAspectFit];
[noteView setFrame:CGRectMake(0, 0, noteSize.width * scale, noteSize.height * scale)];
[containerView addSubview:noteView];
// [noteView release];
}
Thanks,
Subclassing UIImageView is not necessary. Just create a container UIView and add 2 UIImageView subviews.
Edit - this should work for your implementation:
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
UIImage *backgroundImage = [UIImage imageNamed:#"Default-568h"];
CGRect containerRect = CGRectZero;
containerRect.size = [backgroundImage size];
UIView *containerView = [[UIView alloc] initWithFrame:containerRect];
[[self view] addSubview:containerView];
float scale=.1;
UIImageView *backgroundView = [[UIImageView alloc] initWithImage:backgroundImage];
[containerView addSubview:backgroundView];
[backgroundView sizeToFit];
UIImage *noteImage = [UIImage imageNamed:#"note"];
CGSize noteSize = [noteImage size];
UIImageView *noteView = [[UIImageView alloc] initWithImage:noteImage];
[noteView setContentMode:UIViewContentModeScaleAspectFit];
[noteView setFrame:CGRectMake(0, 0, noteSize.width * scale, noteSize.height * scale)];
[containerView addSubview:noteView];
}

Make a rectangle equal to image view

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];

Setting a different background image in a subclass in viewDidLoad

We are using a base class where we set the background picture. That code looks like:
UIImage* image = [UIImage imageNamed:#"App_Background.png"];
UIImageView *imageView = [[UIImageView alloc] initWithImage:image];
imageView.contentMode = UIViewContentModeScaleAspectFill;
CGRect bounds = self.view.bounds;
int offset = 0;
imageView.frame = CGRectMake(0, bounds.size.height - image.size.height + offset, image.size.width, image.size.height);
imageView.autoresizingMask = UIViewAutoresizingFlexibleTopMargin;
imageView.tag = BACKGROUND_TAG;
[self.view insertSubview:imageView atIndex:0]; // insert it behind all other views in NIB
self.background = [imageView autorelease]; // cleanup
If I want to use this as my superclass for another UIViewController but do not want to use the backgound, how do I do that? I thought I could do this in my subclass' viewDidLoad:
UIView *theBackgroundView = [self.view viewWithTag:BACKGROUND_TAG ];
theBackgroundView = nil;
But that doesn't work. Any thoughts? thanks.
Make sure you call [super viewDidLoad] first. Then do [[self.view viewWithTag: BACKGROUND_TAG] removeFromSuperview].

How to add an image subview to a tableview programmatically

I'm new to programming, and I know how to add a static image behind a tableView in a .xib file, but not in code. I have tried the following code:
UIImage * image = [UIImage imageNamed:#"pic.png"];
[self.view addSubView: image];
but nothing happens when I write this!
To display an UIImage you have to use an UIImageView and put it under the UITableView using the same frame. Don't forget to set the table view's background to transparent.
UIImageView *iv = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"pic.png"]];
iv.frame = CGRectMake(0.0, 0.0, 300.0, 600.0);
UITableView *tv = [[UITableView alloc] initWithFrame:iv.frame style:UITableViewStylePlain];
tv.opaque = NO;
tv.backgroundColor = [UIColor clearColor];
[self.view addSubView: iv];
[self.view addSubView: tv];
[iv release];
[tv release];
You need to use a UIImageView, not a UIImage. Also, when adding a view programmatically, don't forget to set the frame.

how to show image on right hand side of table view for iphone

i can show image on left hand side of table view but why my right hand side is not working
CGRect frame;
frame= CGRectMake(310 ,10, 50, 50);
UIImageView * myImageView = [[UIImageView alloc]init];
cell.myImageView.image = [UIImage imageNamed:#"bowl.png"];
myImageView.frame = frame;
[myImageView release];
Try this, it works for me:
UIImageView *image = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"yellow_dot.png"]];
cell.accessoryView = image;
Do this code in willDisplayCell and it should work. Also remember the default cell already has an imageview, you can just move its frame.