UIImageView is not showing correctly - iphone

At this moment, my program looks like this:
As you can see, the square with the rounded rectangles is an image but it doesn't show very well and am not sure how to solve this... Here is the class: http://pastebin.com/QPXbG1uK
The background window gets added here:
UIImageView* background = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"popupWindowBack.png"]] ;
background.center = CGPointMake(_bigPanelView.frame.size.width/2, _bigPanelView.frame.size.height/2);
[_bigPanelView addSubview: background];
Also what do I need to change if I want a simple view as the background instead of the image?

In Your place I would stop using Center coordinates and specifically set it's frame rectangle:
background.contentMode = UIViewContentModeScaleAspectFit;
background.frame = CGRectMake(10,10,_bigPanelView.frame.size.width-20, _bigPanelView.frame.size.height-20);
In this case - imageview will be 10 pix from top/left/right/bottom.
Remove this:
background.center = CGPointMake(_bigPanelView.frame.size.width/2, _bigPanelView.frame.size.height/2);

If you want to achieve your image fit your UIImageView try this:
background.contentMode = UIViewContentModeScaleAspectFit;
Also you need to set background frame to fit screen size, for example if you adding UIImageView in UIViewController class instance:
background.frame = self.view.frame;

i think your image size is larger then bigPanelView view . plz set the background frame size of the bigPanelView. like
background.frame = bigPanelView.view.frame;

Related

Changing UITableViewCell width to fit a UIImageView outside of the cell

I would like to know the best way/correct way to achieve from following layout? I want to place an UIImageView outside of UITableViewCell in a UITableView with static cells.
I have done the following by subclassing UITableViewCell for the cells in section 1 using the following code
- (void)setFrame:(CGRect)frame {
frame.size.width -= 125.0;
[super setFrame:frame];
}
In the UITableViewController's viewDidLoad I add the UIImageView and position it according to the width of the custom UITableViewCell.
This sort of works, but i'm not sure how to deal with rotation and also if what i've done so far would be the correct way?
there are differnt ways to do it. one is to set the width of table view less as you showd in pic 2nd is to use custom table view cell and on required cell add image so that your cell data as well as image will be shown. i think custom cell would be the better solution. tell me if you are asking the same thing what i answered, if no, then i review my answer thank.
I managed to produce what I wanted using the follow, this is proberly not the best way or cleanest way but as no one from StackOverFlow gave any better suggestions I thought I better answer this.
I subclassed the first 3 UITableViewCells and set a frame size to take into account the size of my image.
- (void)setFrame:(CGRect)frame {
float cellWidth = (frame.size.width - 345);
frame.size.width = cellWidth;
[super setFrame:frame];
}
Then in my UITableViewController's viewDidLoad I create and position the UIImageView using the first static cell in the tableview as an IBOutlet ("firstCell"). I then set the autoResizingMask which sorts out rotation and finally add the UIImageView to the view.
//Create and position the image view
UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake((firstCell.frame.size.width), (firstCell.frame.origin.y + 70), 300, 137)];
// Add border and round corners of image view to make style look a little like tableviewcells
[imageView.layer setBorderColor:[UIColor colorWithWhite:0 alpha:0.5].CGColor];
[imageView.layer setBorderWidth:2.0];
[imageView.layer setCornerRadius:5.0];
[imageView setClipsToBounds:YES];
//Set the image in the image view
UIImage *image = [UIImage imageNamed:#"defaultPicture.png"];
imageView.image = image;
//Set resizing of image view for when view is rotated
imageView.contentMode = UIViewContentModeRedraw;
imageView.autoresizingMask = UIViewContentModeScaleAspectFit;
//Add tap gesture for imageview to initiate taking picture.
imageView.userInteractionEnabled = YES;
UITapGestureRecognizer *imageViewTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(takePicture:)];
[imageView addGestureRecognizer:imageViewTap];
//Add image view to view
[self.view addSubview:imageView];
This has not been fully tested and i'm sure isn't a great implementation, but its a starting point for the effect i'm after. Please reply if you know of a better way.
Note: The above code is written for my app on the iPad but screenshots are from testing I did on iPhone

UIView create a inner shadow

I would like to obtain a UIView like this :
But i don't know, what is the best way to do this ?
Is this the best way: https://stackoverflow.com/a/9214570/1228634 ?
Create the shadow as a transparent layer at a particular size, also create a stretchable image, like this:
UIImage *shadowImage = [UIImage imageNamed:#"shadow.png"];
shadowImage = [shadowImage stretchableImageWithLeftCapWidth:floorf(shadowImage.size.width/2) topCapHeight:floorf(shadowImage.size.height/2)];
Put the image in a UIImageView with contentMode as scale to fit.
Call your view as "sView". You can add the shadow like this:
UIImageView *shadowImgView = [[UIImageView alloc] initWithImage:shadowImage];
shadowImgView.contentMode = UIViewContentModeScaleToFill;
shadowImgView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
shadowImgView.frame = sView.bounds;
[sView shadowImgView];
[shadowImgView release]; // only needed if you aren't using ARC
You can try to use several pictures (as it works for html), but I don't think it is better than your example:
you need 4 small pictures to make a texture for top, left, right and bottom border
you need 4 pictures for your View's corners;
draw to make a border from first 4 images using repeating;
draw corner images.

Have UIImage fill the whole frame of the UIImageView

I am working on a small game project, and i have created an image that is 25 x 35 Width / height. Now, if i create a UIImageView with a frame that exactly matches the image, and set the image of that object to the image named above, i thought it would fill it all, but it doesn't, not even by a long shot. It's like there is this huge rectangle, and the image is just added in the middle, so it only fills like 50% of it? any way i can make the image fit exactly?
I tried making the background with a UIColorWithPatternImage, but still doesn't work.
Thanks on advance
/JBJ
Try this:
imageView.contentMode = UIViewContentModeScaleAspectFill;
imageView.image = image;
imageView.frame = frame;
[imageView sizeToFit];
if you have a 25x35 image and 25x35 UIImageview everything must be ok.
Please post some line of code.
If you want to make a game have considered engine like cocos2d? (http://www.cocos2d-iphone.org/)
imageView.image = image;
imageView.bounds = CGRectMake(0,0,image.size.width,image.size.height);

use Aspect Fit mode with Draw-in-Rect method

i want to use Aspect Fit Mode in Draw in Rect Method.
[mainImageView.image drawInRect:CGRectMake(0, 0,mainImageView.frame.size.width,mainImageView.frame.size.height)];
when i show an image in MainImage View it show in scale to fill mode . I want to show it in Aspect to Fit Mode. how can i do it.can someone help me.
Implement it in below way:
UIImageView *imgBg = [[UIImageView alloc] initWithFrame:CGRectMake(0.0, 0.0, 195.0, 39.0)];
[imgBg setImage:[UIImage imageNamed:#"bg_dropdown.png"]];
[imgBg setContentMode:UIViewContentModeScaleAspectFit];
Let me know in case of any difficulty.
Cheers
Probably you forgot to mention
[self addSubview:imgBg];
at the end of the code
If all what you want is just:
I want to show it in Aspect to Fit Mode
You can set content mode of your Image View:
mainImageView.contentMode = UIViewContentModeScaleAspectFit;
But if you want your image to be drawn in the graphics context with that content mode, you can draw the whole view instead of using drawInRect:
mainImageView.contentMode = UIViewContentModeScaleAspectFit;
[mainImageView drawViewHierarchyInRect:view.bounds
afterScreenUpdates:NO];

UIImageview animation problem?

i have one imageview in IB.i linked it perfectly in my view controller's IBoutlet
which is as IB_img .i have done in my viewdidload as
self.IB_img.animationImages = [NSArray arrayWithObjects:
[UIImage imageNamed:#"img_1.jpg"],
[UIImage imageNamed:#"img_2.jpg"],nil];
self.IB_img.animationDuration = 1;
self.IB_img.animationRepeatCount = 0;
[self.IB_img startAnimating];
it is not working perfectly....image is not in screen..but if i do it in without IB,in other words dynamically...it works perfectly...any help please to do animation with IB ?
You would have not set the Mose of the imageView as in required format.
You can do it as:
Try to set the one of the animation images as the image of your image view in xib(temporarily).
then set the image view's mode as strecthtofit or whatever suits you. now remove the image. and build you will see the image in proper position.