UIView background image resize in proportion - iphone

I have a Background.png image with size of 200*100, and i set background image for a uiview as:
UIView view= UIView alloc] initWithFrame:CGRectMake(50, 100, 200, 100)];
view.backgroundColor = [[UIColor alloc] initWithPatternImage:[UIImage imageNamed:#"Background.png"]];
Now, I reset the frame UIView by:
[view setFrame:CGRectMake(50,100,400,200)];
I need to resize the background image in proportion of the uiview, but i have only one fixed size Background.png image. what shall i do.

Take your size accordingly
UIGraphicsBeginImageContext( newSize );
[image drawInRect:CGRectMake(0,0,newSize.width,newSize.height)];
UIImage* newImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
UIView view= UIView alloc] initWithFrame:CGRectMake(50, 100, 200, 100)];
view.backgroundColor = [[UIColor alloc] initWithPatternImage:newImage];
Else take an imageview and set contentMode, If u dont want to tile images

Depending on what your needs are, it might be simplest just to add a UIImageView as a subview of your view, and set its image, letting it handling the resizing for you. Otherwise, you'll probably have some complex resizing and drawing code to write.

Related

How to repeat image horizantly in dynamic creation in ios

Could any one help me i want to add same image multiple times horizontally with same height and width. Important thing is i am creating image view dynamically i want to use same image view for all images! This is image i want to make horizontally like this but only one row needed like this.
You could achieve this by using stretchableImageWithLeftCapWidth:
UIImage *backgroundImage = [[UIImage imageNamed:#"SheetBackground.png"] stretchableImageWithLeftCapWidth:0.5 topCapHeight:0];
As per your request:
UIImage *backgroundImage = [[UIImage imageNamed:#"q4Ses.png"] stretchableImageWithLeftCapWidth:0.5 topCapHeight:0];
[_scro setBackgroundColor:[UIColor colorWithPatternImage:backgroundImage]];
And using your image:
The output is:
You can set this image on top of either UIScrollview, UIView and buttons. You do not need a for loop for that.
UPDATE:
The above code is for filling the entire background. If you wish to add only for one row then you have to create one UIView and set its colorWithPatternImage like below:
UIImage *backgroundImage = [[UIImage imageNamed:#"q4Ses.png"]
stretchableImageWithLeftCapWidth:1 topCapHeight:0];
UIView *v=[[UIView alloc]
initWithFrame:CGRectMake(0, 0, _scro.frame.size.width, 45)];
[v setBackgroundColor:[UIColor
colorWithPatternImage:backgroundImage]];
[_scro addSubview:v];
And the output:
Make a view of the height of image. But this view can have any width.
Then set your tile image in this view with following code.
UIImage *tiledImage = [UIImage imageNamed:#"myTiledImage.png"];
self.view.backgroundColor = [UIColor colorWithPatternImage:tiledImage];
This will get you the image tiled multiple times horizontally.
If the view spreads the image everywhere on screen then you'll have to add the following code to your view
self.view.clipToBounds = YES;
UIScrollView *myScrollView=[[UIScrollView alloc]initWithFrame:CGRectMake(0, 0, 320, 480)];
CGFloat scrollWidth = 0.f;
for (int i=0;i<10;i++)//i=10,put as many image number u want to display
{
imageView = [[UIImageView alloc] initWithFrame:
CGRectMake(scrollWidth, 0, 80, 60.f)];
imageView.image=[UIImage imageNamed:#"urimagename"];
imageView.tag=i;
[myScrollView addSubview:imageView];
scrollWidth += 100;
}
myScrollView.contentSize = CGSizeMake(scrollWidth, 100);
EDIT:
You can achieve this in one more way.
CodenameLambda1's answer is better than the above one.But still some changes needs to be done in the #CodenameLambda1's answer..as the SOP's requirement is to display it in scrollview.So instead of self.view use scrollview.
UIScrollView *vie=[[UIScrollView alloc]initWithFrame:CGRectMake(0, 0, 320, 60)];
UIImage *tiledImage = [UIImage imageNamed:#"login"];
vie.backgroundColor = [UIColor colorWithPatternImage:tiledImage];
vie.contentSize=CGSizeMake(1400,60);
vie.clipsToBounds = YES;
[self.view addSubview:vie];

How to set image for UIBarButtonItem Flexible space - IOS

Have a look at this image...
I want to design toolbar like this, Each UIBarButtonItem should be separated by a thin line. So I have inserted flexible space button in between each UIBarButtonItem (that helps me in aligning UIBarButtonItem while orientation changes), however if I set an image for that, its no more acting as flexible button, changes to normal UIBarbutton.
So I need an flexible space button with an image.. Any idea how to do it.
(OR)
How to design a UIToolbar like above... Any helps is really appreciated. Thanx
To set background image for UIBarbuttonItem
UIImage *faceImage = [UIImage imageNamed:#"facebook.png"];
UIButton *face = [UIButton buttonWithType:UIButtonTypeCustom];
face.bounds = CGRectMake( 0, 0, faceImage.size.width, faceImage.size.height );
[face setImage:faceImage forState:UIControlStateNormal];
UIBarButtonItem *faceBtn = [[UIBarButtonItem alloc] initWithCustomView:face];
I think this will help you..
UIImage* topImage = [UIImage imageNamed:#"TabBarGradient.png"];
// Create a new image context
UIGraphicsBeginImageContextWithOptions(CGSizeMake(width, topImage.size.height*2), NO, 0.0);
// Create a stretchable image for the top of the background and draw it
UIImage* stretchedTopImage = [topImage stretchableImageWithLeftCapWidth:0 topCapHeight:0];
[stretchedTopImage drawInRect:CGRectMake(0, 0, width, topImage.size.height)];
// Draw a solid black color for the bottom of the background
[[UIColor blackColor] set];
CGContextFillRect(UIGraphicsGetCurrentContext(), CGRectMake(0, topImage.size.height, width, topImage.size.height));
// Generate a new image
UIImage* resultImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return resultImage;
and for more detail you can check this...
http://idevrecipes.com/2011/01/04/how-does-the-twitter-iphone-app-implement-a-custom-tab-bar/
If didn't solve your problem..let me know..

Display Top Portion of UIImageview

I'm trying to display a top portion of an UIImage.
I know I can use the following code and display the middle portion. I'm just trying to figure out how to do the same for the top portion:
imageView.contentMode = UIViewContentModeScaleAspectFill;
imageView.clipsToBounds = YES;
The UIImage I get from the server is not necessarily the same size, so I cannot use UIViewContentModeTopLeft. I want a way to scale the image and show the top portion.
Thanks.
You want to display an arbitrary rectangle of your image, so the easiest way to go will to be to put your imageView inside a regular view. You can set the frame of the image view to display the bit you want and set the enclosing view to do the clipping.
e.g. you have a 1000 x 1000 px image and you want to display the rectangle 200,200 to 400,400
UIImageView* imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"SomeImage"]];
imageView.frame = CGRectMake( -200, -200, 1000, 1000);
UIView* enclosingView = [[UIView alloc] initWithFrame:CGRectMake( 0, 0, 200, 200);
enclosingView.clipsToBounds = YES;
[enclosingView addSubview: imageView];
Content mode doesn't matter much in this case, since you're setting the dimension of the image view to match the image.
One way is to call CGImageCreateWithImageInRect on your original image to create a new image with just the top portion.
Something like:
CGImageRef newImageRef = CGImageCreateWithImageInRect( oldImage.CGImage, CGRectMake(0,0,50,50) );
UIImage *newImage = [UIImage imageWithCGImage:newImageRef];
CGImageRelease( newImageRef );
then display newImage rather than oldImage.

how can I use UIImageView on full screen in UIViewController?

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

UIImageView change Width and Height

I've read various posts on here asking similar questions... I've tried various ways that were posted including bounds and frames etc. including the following:
myImage.frame = CGRectMake(0.0f, 0.0f,50.0f, 50.0f);
and:
myImage.bounds = CGRectMake(0.0f, 0.0f,50.0f, 120.0f);
neither of those work.
However, I find it interesting that the following code let's me move the Image around but doesn't change the width:
CGRect frameRect = myImage.frame;
frameRect.size.width = 50.0f;
frameRect.origin.x += 10.5f;
myImage.frame = frameRect;
So why don't any of these change the width/height of my ImageView?
I found another post on here that basically states I have to right a small book of code to get it resize my image... is that true?
Such as this one:
UIImage: Resize, then Crop
certainly this is simpler than that??
The following will change the size of the UIImaveView, clipping the underlying image without resizing it and keeping it aligned to bottom left of view:
imageView.frame = CGRectMake(
imageView.frame.origin.x,
imageView.frame.origin.y, newWidth, newHeight);
imageView.contentMode = UIViewContentModeBottomLeft; // This determines position of image
imageView.clipsToBounds = YES;
First off, you can't set the frame or bounds of the UIImage - that will only work on a UIImageView.
I've found that changing the frame of a UIImageView causes the Image to be scaled to the new size. Sometimes, that's undesirable - and you want instead to crop the image.
I can't tell if this is what you're asking for, but here's some code to crop an image to a specific size in a UIImageView:
UIImage *myImage = [UIImage imageNamed:#"photo.png"];
CGRect cropRect = CGRectMake(0.0, 0.0, 320.0, 44.0));
CGImageRef croppedImage = CGImageCreateWithImageInRect([myImage CGImage], cropRect);
UIImageView *myImageView = [[UIImageView alloc] initWithFrame:cropRect];
[myImageView setImage:[UIImage imageWithCGImage:croppedImage]];
CGImageRelease(croppedImage);
From what I get of the question, the OP wanted to change the size of the UIImageView when the size of the container UIView is changed. The code below will do it...
UIView * foo = [[[UIView alloc] initWithFrame:CGRectMake(0, 0, 25, 25)] autorelease];
foo.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
UIImageView * bar = [[UIImageView alloc] initWithImage:
[UIImage imageNamed:#"test.png"]];
bar.autoresizingMask = foo.autoresizingMask;
[foo addSubview:bar];
[self.view addSubview:foo];
The key here are the foo.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight and the bar.autoresizingMask = foo.autoresizingMask; lines. Forget either of these, and the whole jigmarole will stop working.
Well, if your read the documentation about UIIMage you can notice that is impossible to change any parameter of an UIImage after create it, the solution I've implemented for use high quality images for change some parameter of (for example) the SliderControl as Screw Image, is the next one:
UIImage *tumbImage= [UIImage imageNamed:#"screw.png"];
UIImage *screw = [UIImage imageWithData:UIImagePNGRepresentation(tumbImage) scale:2];
With that, I can to use 100x100 px image in my apps scaled to 50%.
Kind regards.
Try Using a UIScrollView. Add the UIImageView to the UIScrollView in Interface Builder you can then control the position and size of the image as follows:
CGRect rect = [scrollView frame];
rect.origin.x = 50.0f;
rect.origin.y = 0.0f;
rect.size.width = 320.0f;
rect.size.height = 150.0f;
[scrollView setFrame:rect];
If you tried those methods cannot work, the only way to do it is to add the constraint of width and height to the UIImageView.
// Create the constraint in code
NSLayoutConstraint *constraint0 = [NSLayoutConstraint constraintWithItem: myImage attribute:NSLayoutAttributeWidth relatedBy:NSLayoutRelationEqual toItem:nil attribute:NSLayoutAttributeWidth multiplier:1.0f constant: yourNewsWidth];
NSLayoutConstraint *constraint1 = [NSLayoutConstraint constraintWithItem: myImage attribute:NSLayoutAttributeHeight relatedBy:NSLayoutRelationEqual toItem:nil attribute:NSLayoutAttributeHeight multiplier:1.0f constant: yourNewsHeight];
[myImage addConstraint:constraint0];
[myImage addConstraint:constraint1];
Use myImageView.frame = myNewPosAndSize; to resize or reposition your image view (as with any other view). It might confuse you that the image view draws its image with its original size, possibly exceeding its own dimensions. To disable this use myImageView.clipsToBounds = NO;
You don't have to write a whole book, you can copy that code.
I believe the UIImageView always draws the image at 0,0 at a 1.0 scale. You'll need to resize the image if you want to continue using the UIImageView.