Stacking views ... code review needed - objective-c

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.

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

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.

UIGestureRecognizer's sender view not working properly

According to this question, the UIGestureRecognizer has a view property which refers to the view the gesture is attached to. I used this in my code like this:
//Code for the 1st UIScrollView
UIImageView *bookCover = [[UIImageView alloc]initWithFrame:CGRectMake(50, 100, 145, 420)];
UITapGestureRecognizer *singleTap = [[UITapGestureRecognizer alloc]initWithTarget:self
action:#selector(downloadBookTapped:)];
[bookCover addGestureRecognizer:singleTap];
[bookCover release];
[singleTap release];
//Code for the second UIScrollView
UIImageView *fileCover = [[UIImageView alloc]initWithFrame:CGRectMake(50, 100, 145, 420)];
UITapGestureRecognizer *singleFileTap = [[UITapGestureRecognizer alloc]initWithTarget:self
action:#selector(downloadFileTapped:)];
[fileCover addGestureRecognizer:singleFileTap];
[fileCover release];
[singleFileTap release];
And here is where I user the view property:
- (void)downloadBookTapped:(UITapGestureRecognizer *)sender
{
UIImageView *imgView = (UIImageView *)sender.view;
CGRect rect = [imgView frame];
UIImageView *images = [[UIImageView alloc]initWithFrame:rect];
//rest of code here...
}
- (void)downloadFileTapped:(UITapGestureRecognizer *)sender
{
UIImageView *imgView = (UIImageView *)sender.view;
CGRect rect = [imgView frame];
UIImageView *images = [[UIImageView alloc]initWithFrame:rect];
//rest of code here...
}
The problem here is that I have two scrollView and each scrollview holds multiple books. When I select a book at the 1st scrollView, the images is displayed correctly. But when I select a book inside the 2nd scrollView, the images is displayed incorrectly. Can anyone explain why this happens? Thanks.
---ADDITIONAL INFO---
The two scrollViews have the same width and height. The difference, of course, is there placement. The first scrollView is placed at (0, 0), while the second is at (0, 350). You can imagine the two as "shelves", the first one being the top shelf and the second one being the bottom shelf.
To specify the problem, say that I selected a book inside the second scrollView. The images will then be displayed as if I selected a book in the 1st scrollView. Meaning, the images is displayed in the 1st scrollView instead of the second scrollView.
Because the gestureRecognizer is bound to the first UIImageView and not the second.
[bookCover addGestureRecognizer:singleTap];
Do this for your other UIImageView and you will get the results you want.
I know now what I did wrong! Instead of adding the images as the subview of the scrollViews, I did this:
[self.view addSubView:images];
That's why It keeps appearing on the top side. It should be like this:
[scrollBook addSubview:images];
[scrollFile addSubView:files];

set background image dimensions IOS

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)

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.