When I use the following code to set the background of my UITableView to an image, about 20-30px of the image does not draw at the very bottom of the screen. I'm very puzzled by this as the image size is 480 high while the view is 460 high. The image should actually be sizing down to just barely fit the screen. Could somebody enlighten me? Thanks a lot
UIImageView *imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"Default.png"]];
self.tableView.backgroundView = imageView;
Set the frame of the background image to this:
{{0, -20}, {320, 480}} either in IB or in code.
ie
imageView.frame = CGRectMake(0, -20, 320, 480);
the high of of all the screen is 480 px and the statusBar has 20 px so your view has 460 px
Related
I have UIImageView in which I'm showing 50x100 image.
I want to show only a part of image 50x50 (top part)?
How can I do that?
The very simple way to move big image inside UIImageView as follows.
Let we have the image of size (100, 400) representing 4 states of some picture one below another. We want to show the 2nd picture having offsetY = 100 in square UIImageView of size (100, 100).
The solution is:
UIImageView *iView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 100, 100)];
CGRect contentFrame = CGRectMake(0, 0.25, 1, 0.25);
iView.layer.contentsRect = contentFrame;
iView.image = [UIImage imageNamed:#"NAME"];
Here contentFrame is normalized frame relative to real UIImage size.
So, "0" means that we start visible part of image from left border,
"0.25" means that we have vertical offset 100,
"1" means that we want to show full width of the image,
and finally, "0.25" means that we want to show only 1/4 part of image in height.
Thus, in local image coordinates we show the following frame
CGRect visibleAbsoluteFrame = CGRectMake(0*100, 0.25*400, 1*100, 0.25*400)
or CGRectMake(0, 100, 100, 100);
You can crop the image by using CGImageCreateWithImageInRect, which is Quartz primitive working on CGImageRef, so you would have something like:
CGImageRef imageRef = CGImageCreateWithImageInRect(originalImage.CGImage, cropRect);
UIImage* outImage = [UIImage imageWithCGImage:imageRef scale:originalImage.scale orientation:originalImage.imageOrientation]];
CGImageRelease(imageRef);
When calculation cropRect, keep in mind that it should be given in pixels, not in points, i.e.:
float scale = originalImage.scale;
CGRect cropRect = CGRectMake(0, 0,
originalImage.size.width * scale, originalImage.size.height * 0.5 * scale);
where the 0.5 factor accounts for the fact that you want the top half only.
If you do not want to go low-level, you could add your image to a UIView as a background color and use the clipToBounds CALayer property to make the clipping:
myView.backgroundColor = [UIColor colorWithBackgroundPattern:myImage];
myView.layer.clipToBounds = YES;
also, set myView bounds accordingly.
I might have a solution for you. See if this works. In Interface Builder there is an option about Image content fill properties. You can set it to top-left. Follow the image in interface builder -
After this set the size of UIImageView to 50x50 with "clip-subviews" checked...
Here in my app i used the background image size as 320 x 480,but in the end of the screen some portions not visible,here my code
self.view.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:#"ibg.png"]];
Please help me to solve to make the image screen fit..
If the invisible portion has height equal to 50 pixel, then could you please try resize your image (ibg.png) to 320 x 430 using
- (UIImage *)imageWithImage:(UIImage *)image convertToSize:(CGSize)size {
UIGraphicsBeginImageContext(size);
[image drawInRect:CGRectMake(0, 0, size.width, size.height)];
UIImage *destImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return destImage;
}
after that
self.view.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:#"ibg.png"]];
should make it, hope it help, please give me a feedback, thanks.
Your image may be offset by the status bar, which takes up 20 pixels (or 'points') of space at the top of the screen. If your status bar is visible, the Y position of your full-screen background image must be -20 rather than 0.
i have a table view that i want to make a custom uinavigationn bar i tried this code
UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0.0, 0.0, self.view.bounds.size.width, self.view.bounds.size.height)];
//here for v, width= navBar width and height=navBar height
//
[view setBackgroundColor:[UIColor colorWithPatternImage:[UIImage imageNamed:#"newsBanner.png"]]];
[self.navigationController.navigationBar addSubview:view];
the problem is the image is not centered cause its size is 640 x 72... is there a way to make it fit?
Your nav bar should only be 44 pixels tall. On top of that, it is maximum 320 points wide (640 px on retina devices, 320 on non-HD devices). So the size of your image is way too big. Make an image that has 320x44 dimensions, and then do this:
UINavigationBar *theBar = self.navigationController.navigationBar;
if ( [theBar respondsToSelector:#selector(setBackgroundImage:forBarMetrics:)] ) {
UIImage *image = [UIImage imageNamed:#"your image here"];
[theBar setBackgroundImage:image forBarMetrics:UIBarMetricsDefault];
}
Of course that will only work on iOS 5. You'll need to find other ways of handling setting the background on OS below 5.0. But there are plenty of stack overflow questions that address that issue :)
I am developing a software and in it I am making a new file, which is subclass of UIViewController, when I do use UIImageView, then there is little border remaining in it, what should I do so that my image should cover all area around it,
my coding of UIImageView in ViewDidLoad is
UIImageView* view = [[UIImageView alloc] initWithImage: [UIImage imageNamed: #"frontnew.png"]];
view.frame = CGRectMake(0, 0, 320, 415);
[self.view addSubview:view];
now what should I do ????
If I increase height and width then it will increase from two sides, not all ???
you should set imageview property as scalltoFit
May be it can help you
I have a bunch of UIImageViews that are in different proportions. Some of 100x101 some are 130x121.
How can I mask these to 80x80 and NOT stretch the images? I basically just want to mask a square out of each one. (kind of like the Apple's Photo thumbnail view does)
Set the image view's size to 80 x 80
set the image view's contentMode property to UIViewContentModeScaleAspectFill
Finally, to make round corners, use the following code, and import QuartzCore/QuartzCore.h at the beginning of your implementation file.
CALayer * layer = [myImageView layer];
[layer setMasksToBounds:YES];
[layer setCornerRadius:12.0f];
Edited: Yes, by saying size I mean frame, the W and H:
Set its content mode UIViewContentMode, you may be looking for UIViewContentModeScaleAspectFit or UIViewContentModeScaleAspectFill.
UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 80, 80)];
[imageView setContentMode:UIViewContentModeScaleAspectFit];
[imageView setImage:[UIImage imageNamed:#"myImage.png"];
.
.
.