set background image dimensions IOS - objective-c

I have an image bigger then a normal iPhone screen that I want to set as my background image but when I load the simulator I just see part of the image. Here is how I assigned the image:
UIImage *background = [UIImage imageNamed:#"background.png"];
[self.view setBackgroundColor:[UIColor colorWithPatternImage:background]];
how can I set the background image to something like self.view.bounds? If I was to use a UIImageView how do I ensure that it is in the background!!!

Make a UIImageView, give the UIImage to the UIImageView and add the UIImageView as a child to the view. Like so:
UIImageView *backgroundView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"name.png"]];
[backgroundView setFrame:CGRectMake(0, 0, 320, 480)];
[self.view insertSubView:backgroundView atIndex:0];
[backgroundView release];

Set uiimageview frame - this is limitations for display.
Use
background.frame = CGRectMake (0,0, 320, 480); // as example - full screen,
also like in post above add addSubview and release code.
If you used frame - coordinates calculate from your superview - in that case - from self.view (simulator display). If you used bounds, coordinates calculate relatively to yours uiimageview (not self.view)

Related

UIImageView - UIScrollView Fill and set X to 0

StackOverflow.. last resort as I hate bothering other devs but I've come to the end of my tether with my problem..
I have a UIView, in which are 2 UIScrollViews - in each scrollview is a UIImageView. The original idea is that I can move the image in each scroll view on the Y Axis to reposition the images.
Currently, the UIImageViews are using the content mode of (so the images don't stretch and display in full):
UIViewContentModeScaleAspectFill
The images load fine into the ScrollViews - the only problem is, the position of the UIImageView's are centred on the Y Axis and I need them to be 0! I've tried setting the UIImageView frame after adding and setting the content mode but nothing.
Here is my code:
- (outletContainer is a UIView)
- (imagePack is an NSMutableArray of UIImages)
UIImageView *tempFirst = [[UIImageView alloc] initWithImage:[imagePack objectAtIndex:0]];
[tempFirst setFrame:CGRectMake(0, 0, outletContainer.frame.size.width, outletContainer.frame.size.height/2)];
[tempFirst setContentMode:UIViewContentModeScaleAspectFill];
// add scrollview for positioning:
UIScrollView *firstScroller = [[UIScrollView alloc] init];
[firstScroller setFrame:CGRectMake(0, 0, tempFirst.image.size.width, outletContainer.frame.size.height/2)];
[firstScroller setContentSize:tempFirst.image.size];
[firstScroller addSubview:tempFirst];
[outletContainer addSubview:firstScroller];
UIImageView *tempSecond = [[UIImageView alloc] initWithImage:[imagePack objectAtIndex:1]];
[tempSecond setFrame:CGRectMake(0, 0, outletContainer.frame.size.width, outletContainer.frame.size.height/2)];
[tempSecond setContentMode:UIViewContentModeScaleAspectFill];
// add scrollview for positioning:
UIScrollView *secondScroller = [[UIScrollView alloc] init];
[secondScroller setFrame:CGRectMake(0, outletContainer.frame.size.height/2, tempSecond.image.size.width, outletContainer.frame.size.height/2)];
[secondScroller setContentSize:tempSecond.image.size];
[secondScroller addSubview:tempSecond];
[outletContainer addSubview:secondScroller];
Thanks in advance!
I think you have to delete these two lines:
[tempFirst setFrame:CGRectMake(0, 0, outletContainer.frame.size.width, tempFirst.image.size.height*tempF)];
[tempSecond setFrame:CGRectMake(0, 0, outletContainer.frame.size.width, outletContainer.frame.size.height/2)];
When you create an UIImageView with image it takes UIImage size.
Edited:
Ok, then you have to do that:
[tempFirst setFrame:CGRectMake(0, 0, outletContainer.frame.size.width, tempFirst.image.size.height*outletContainer.frame.size.width/tempFirst.image.size.width)];
firstScroller.contentSize = CGSizeMake(outletContainer.frame.size.width, tempFirst.frame.size.height);

UIView changing the size of the image?

I am trying to add UIImage to UIView .
The image is exactly in the size of the view -as i defined the view rect .
Somehow i see that the images is displayed wider,and taller(its stretched ) .
Why does my view is change the image size ?
UIImage *strip=[UIImage imageNamed:#"albumStrip.png"];
UIImageView *imageView=[[UIImageView alloc]initWithImage:strip];
UIView * aView = [ [UIView alloc] initWithFrame:CGRectMake(0.03*winSize.width, 0.85*winSize.height , 0.95*winSize.width, winSize.width/10) ];
aView.backgroundColor = [UIColor whiteColor];
aView.tag = 31000;
aView.layer.cornerRadius=1;
[aView addSubview:imageView];
EDIT :
I can see that my image is 640x960. is it possible that for the iPhone4 the UIImage dont know how to take it and div it by factor 2 ?
Try setting the UIImageView's ContentMode (http://developer.apple.com/library/ios/#documentation/uikit/reference/UIImageView_Class/Reference/Reference.html)
imageView.contentMode = UIViewContentModeScaleAspectFit;
use
imageView.layer.masksToBounds = YES;
which will restrict image to be wide and taller
If the masksToBounds property is set to YES, any sublayers of the layer that extend outside its boundaries will be clipped to those boundaries. Think of the layer, in that case, as a window onto its sublayers; anything outside the edges of the window will not be visible. When masksToBounds is NO, no clipping occurs, and any sublayers that extend outside the layer's boundaries will be visible in their entirety (as long as they don't go outside the edges of any superlayer that does have masking enabled).
of course your image is going to be stretched and can not be shown in your view.
Because its frame is a lot bigger than the size of the UIView.
you should set the frame of the UIImageView to be the same size as your UIView, the add it as a subView:
UIImage *strip=[UIImage imageNamed:#"albumStrip.png"];
CGRect frame = CGRectMake(0.03*winSize.width, 0.85*winSize.height , 0.95*winSize.width, winSize.width/10);
// create the uiimageView with the same frame size as its parentView.
UIImageView *imageView=[[UIImageView alloc] initWithFrame:frame];
// set the image
imageView.image = strip;
// create the view with the same frame
UIView * aView = [ [UIView alloc] initWithFrame:frame];
aView.backgroundColor = [UIColor whiteColor];
aView.tag = 31000;
aView.layer.cornerRadius=1;
[aView addSubview:imageView];
and of course if you can make the size of the uiimageview smaller ;)

ios tableviewcell several objects one a cell programatically , setting prototype cell programatically , custom table view cell style,

hello I built this tableview using the InterfaceBuilder and storyboard I used custom for the style of the cell and content prototype of the table view, this is the image:
custom tableview
but now I need to redo everything programmatically I tried insert an imageview in the cell using this code
UIImageView *imgView = [[UIImageView alloc] initWithFrame:
CGRectMake(300, 0, 80, 80)];
imgView.image = [UIImage imageNamed:#"esquina_estrella.png"];
cell.imageView.image = imgView.image;`
but the image stays static unresponsive to CGRectMake thanks for you help
Use this code to the Cell:
// Make your frame origin 0,0 as below done.
UIImageView *imgView = [[UIImageView alloc] initWithFrame:
CGRectMake(0, 0, 80, 80)];
imgView.image = [UIImage imageNamed:#"esquina_estrella.png"];
[cell addSubview:imgView];
[imgView release];
imgView = nil;
Add
imgView.clipsToBounds = YES;
To limit display of the image to the frame of the image view.
Also, you are adding the image of the image view to the cell's image view, which is a different object, without using your image view and its frame. You actually do not need an image view if you are just using the image. What you want is
cell.imageView = imgView;
or
[cell.contentView addSubview:imgView];
The latter will be preferable if you want to place it exactly and add more image views as in your screen shot.

Subview is displaying outside of its superview

In my iPad application, I have a UIView object named "myView" whose frame is (0, 0, 600, 500). Now I want to add a UIImageView in this myview, so here is my code for doing this:
UIImageView *myImageView = [[UIImageView alloc] initWithFrame:CGRectMake(-100, 0,100, 200)];
[myImageView setImage:UIImage imageNamed:#"myImage.png"];
[myView addSubView:myImageView];
[myImageView release];
So according to this code myImageView should not be displayed as its x-coordinate is "-100" and width is "100". But it is displaying out of myView at left side.
Can anyone please help to resolve this problem?
Use UIView's clipsToBounds property, which is off by default.

Stacking views ... code review needed

The problem:
On top of existing UIView, load a background image
From the card deck image (big image), cut 1 card (a part of a big image) and
Place that card on top of the background image
The following code works. It does exactly what i need it do to. I ask you though ..
Am i doing it right? Is this the way it's done?
Are all steps i used really needed? I have a strong suspicion i made it more complicated then it needs to be
Thank you for your time
// Define overall UIView area and create a view
UIView *background = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 460)];
// Gain access to background image
UIImage *backgroundImage = [UIImage imageNamed:#"green_background.jpg"];
// Put background image on top of a
UIImageView *myImageView = [[UIImageView alloc] initWithImage:backgroundImage];
// So now we have the view of some size with some image bahind it
[background addSubview:myImageView];
// #####################################################################
CGRect positionOnParentView = CGRectMake(40, 40, 50, 130);
CGImageRef bigImage = [UIImage imageNamed:#"cards.png"].CGImage;
CGImageRef partOfABigImage = CGImageCreateWithImageInRect(bigImage, CGRectMake(0, 0, 200, 50));
// get an image to be displayed
UIImage *partOfImage = [UIImage imageWithCGImage:partOfABigImage];
// Put it on it's onw view
UIImageView *cardImage = [[UIImageView alloc] initWithImage:partOfImage];
// create a UIview and add image
UIView *card = [[UIView alloc] initWithFrame:positionOnParentView];
[card addSubview:cardImage];
[background addSubview:card];
[[self view] addSubview:background];
You're creating this view hierarchy:
UIView (self.view)
UIView (background)
UIImageView (backgroundImage - backgroundImage)
UIView (card)
UIImageView (cardImage - partOfABigImage)
A UIImageView can have subviews. So do you need all of those intermediate views? Would this simpler hierarchy be sufficient?
UIImageView (self.view - backgroundImage)
UIImageView (cardImage - partOfABigImage)
A couple of things:
Maybe you just didn't include your release statements, but make sure you are releasing the items you call alloc on. You can also autorelease them. For example,
UIView *background = [[[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 460)] autorelease];
Yes, you are doing it right, the only way you will really reduce the amount of code you have here is to give each card it's own image so that you don't have to crop within the big image.