how to extend Background Image in IPhone? - iphone

i have a UIScrollView, when somebody click a button, the scrollview will become longer. And i would like to make its background image extended also, which means, the buttom part of the image can be duplicated. do you guys know how to do it in Objective c?
thanks a lot!
penny

I think this will be usefull for you.
you can set the scroll view backgroundColor as follows;
scrollView.backGroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:#"moviestrip.png"]];
by doing like this, when the scrollView become longer, automatically the image will be duplicated.
or else without duplicating you want to show the image with more width and height as much as the scrollview content, then follow this code.
CGRect rect = imageView.frame;
rect.size = scrollView.contentSize;
imageView.frame = rect;
by doing like that the imageView will show as much as size of the scrollview.
Regards,
Satya

Related

Objective C / Autoresizing subviews not working

So, I'm trying to make a calendar from an image.
Images are in higher resolution then the screen, so I thought about loading the image to a larger view, adding everything to it (as subviews), resizing the view to fit the screen to show the user, then if he decides to save the image, resizing it again to a larger size and saving it. (It's mostly working as I intended).
The problem I'm having is the subviews I add to my ImageView won't resize.
Loading the image to my ImageView and enabling AutoresizesSubviews:
[self.backgroundImageView setImage:backgroundImage_];
[self.backgroundView setAutoresizesSubviews:YES];
Adding a UILabel on it:
UILabel *calendarLabel = [[UILabel alloc] initWithFrame:CGRectMake(screenWidth_ * 0.1, 0, screenWidth_ * 0.8, screenHeight_ * 0.15)];
[calendarLabel setText:#"Calendar"];
[calendarLabel setTextColor:[UIColor whiteColor]];
[calendarLabel setTextAlignment:NSTextAlignmentCenter];
[calendarLabel setBackgroundColor:[UIColor clearColor]];
[self.backgroundImageView addSubview:calendarLabel];
Saving the size it should be resized to:
savedRect_ = self.backgroundView.frame;
Resizing the imageview (actually resizing the parent of the imageview, since imageView occupies the whole view):
[self.backgroundView setFrame:savedRect_];
Now I have no idea why this isn't working as I intended it.
PS: Did some testing and it turns out it's a little different with the programmatically added views then the ones added with IB. I have a feeling it has to do with AutoresizingMask.
Did some research, and I would imagine I have to make everything flexible (it can grow in width, height, size on top, bottom, right and left sides), but it's still isn't working. If I enable all the 6, the view just doesn't move.
Could anyone tell me what setting I need to make so the views stay at the same position related to their parent View?
Ok, turned out to be an easy solution as I expected...
I tried applying each mask separately:
[calendarLabel setAutoresizingMask:UIViewAutoresizingFlexibleLeftMargin];
[calendarLabel setAutoresizingMask:UIViewAutoresizingFlexibleWidth];
....
Instead of:
[calendarLabel setAutoresizingMask:UIViewAutoresizingFlexibleLeftMargin|UIViewAutoresizingFlexibleWidth|UIViewAutoresizingFlexibleHeight|UIViewAutoresizingFlexibleRightMargin|UIViewAutoresizingFlexibleTopMargin|UIViewAutoresizingFlexibleBottomMargin];

ImageView contentMode to scale images properly?

I'm drawing a bunch of UIImageView (100x100) as menu items. Each has its own image, some are larger than 100x100, some smaller. By default, the imageViews with larger images are being scaled down in ratio to fit the 100x100 box nicely.
However, images smaller than 100x100 are being scaled up, which makes them really blurry. I'd like it so the smaller images simply remain the size they naturally are. Otherwise I'm happy with the way large images are scaling, just not the smaller images.
I haven't been able to achieve anything by switching to UIViewContentModeScaleAspectFit, UIViewContentModeScaleAspectFill, or UIViewContentModeScaleToFill.
Does anyone have ideas or hacks on how I could achieve this? I could use an if statement to check the image's dimensions and sent their contentMode (Center?), but that seems a bit hackish since I'm drawing each item from an NSDictionary with a for in loop. Any help appreciated. Thanks
If I understand correctly you can just check the image dimensions first before setting your frame, something like:
UIImageView *imageView = [[UIImageView alloc] initWithImage:[UIImage imageName:#"myimage.png"]];
[imageView sizeToFit];
CGRect newFrame = imageView.frame;
if (newFrame.size.width > 100) newFrame.size.width = 100;
imageView.frame = newFrame;
You'll need some extra logic to do similar things for the height. Is this what you mean?

stretchableImageWithLeftCapWidth causes corners to be blurry

I have a box that i want to be expanable only on its width, while still maintaining the rounded corners that I have. I made the graphic in photoshop. and it is exactly 13px wide, so 6 for each corner and 1 for the middle to repeat.
UIImage* img = [[UIImage imageNamed:#"screen_displayer_rounded.png"] stretchableImageWithLeftCapWidth:6 topCapHeight:6];
CGRect rect = CGRectMake(272.0f, 14.0f, 100.0f, 30.0f);
UIImageView* imgView = [[UIImageView alloc] initWithFrame:rect];
[imgView setImage:img];
Could anyone tell me why this might be happening?
Thanks!
I have found that you cant just rely stretchableImageWithLeftCapWidth to get it to resize correctly.
I normally use the contentStretch property that is available to all UIView subclasses when stretchableImageWithLeftCapWidth doesnt work for me.
Checkout the UIView apple docs regarding the contentStretch property.
You should check if the view doesn't end up at a non-integer position. If you choose "Run with performance tool > Core Animation" and check "Color misaligned images", all misaligned images will show purple.
If your image is purple, try to find out which superview is causing this. Look out for things being centered, since that is a common cause for these issues.
Just checking - are you perhaps viewing a non-retina image on a retina display? This would definitely stretch it out.

image display in uiscrollview

Am facing a probem with my scrollview.Am displaying the image in a scrollview programatically and i added the zooming and paning effects.When i run the application image is displaying in the scroll view but it is not in centre(like it is displaying at right corner and some part of image is inside the scrollview)After zooming when i scroll the image the image is moving and i can see the black area which i dont want.
Please,somebody can help me
Thanks in advance
here is the code
mainView2 = [[UIScrollView alloc]init];
[mainView2 setFrame:CGRectMake(imageView2.frame.origin.x, imageView2.frame.origin.y, 222, 120)];
[mainView2 setContentSize:CGSizeMake(600,600)];
[mainView2 setMaximumZoomScale:2.0];
[mainView2 addSubview:imageView2];
mainView2.delegate = self;
[self.view addSubview:mainView2];
mainView2.scrollsToTop = NO;
here mainview2 is my scrollview and imageview2 is the uiimageview which i want to display in scrollview
I don't know exactly what you mean "I can see the black Area". However, the "center image" problem is quite a common one. As far as I know, there is no out of the box way to solve this. Fortunately, there is some great code on github which handels image scrolling in a UIScrollView nicely. See this question and this github project for further information.

backgroundColor colorWithPatternImage ends up stretched if the view size changes, instead of repeating?

I'm setting a view background to a repeating image using colorWithPatternImage. If the view size changes after I've set the background the first time, the image gets stretched instead of repeating for the new size - any ideas how I can fix this?
This is what I'm doing:
set the view's background in viewDidLoad:
frameView.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:#"frame-repeat.png"]];
grab some information from the server, update the view, change the view's frame size to fit the new content (usually, this means making the view taller)
background image is now stretched, even if I set the backgroundColor again using the exact same code.
If I don't do the initial background set, and only set it after I retrieve the data from the server and change the view size, it looks great - repeats, and no stretching. However, that means no background while the data is loading... looks ugly.
Has anyone else run into this?
Thanks for any help!
The answer is quite simple, just call
[[self view] setNeedsDisplay];
in the controller when you change the frame size (or whereever you do it, bottom line is to send a setNeedsDisplay message to the view after the frame change).
If this doesn't work - try setting the background color.
And if this doesn't work, implement your own custom view, and in it override the method
- (void) drawRect:(CGRect) rect {
CGRect myBounds = self.bounds;
CGContextRef context = UIGraphicsGetCurrentContext();
[[UIColor colorWithPatternImage:[UIImage imageNamed:#"frame-repeat.png.png"]]set];
CGContextFillRect(context, myBounds);
}
And then, after the resizing, send the setNeedsDisplay to your view. This will work for 100%.
Hope this was helpful, Pawel