I have the following in my Navigation Controller Load event...
UIImage *image = [UIImage imageNamed:#"Background.jpg"];
CGImageRef cgImg = CGImageCreateWithImageInRect(image.CGImage, CGRectMake(0, 0, 500, 620));
UIImage* part = [UIImage imageWithCGImage:cgImg];
UIImageView* imageView = [[UIImageView alloc] initWithImage:part];
[self.view insertSubview:imageView atIndex:0];
This sets the background image and works fine once I have set the embedded view to have a clear background. My question is how do I alter this image once it is loaded?
The effect I am looking for is that I want to crop a new section of the image when different views are pushed onto the stack.
I know I could load a new image for each table but the desired effect I want is so that when you push to the next view, the table moves from right to left showing the new table, but the background moves right to left only a little bit.
Ended up using NSTimer and this combined
[[self.navigationController.view.subviews objectAtIndex:0] removeFromSuperview];
[self.navigationController.view insertSubview:imageView atIndex:0];
Use tag to different your backgroundView and other views
#deifne BACKGROUNDVIEWTAG 999
if(![self.view subviews] || ((UIView *)[[self.view subviews] objectAtIndex:0]).tag != BACKGROUNDVIEWTAG)
{
UIImage *image = [UIImage imageNamed:#"Background.jpg"];
CGImageRef cgImg = CGImageCreateWithImageInRect(image.CGImage, CGRectMake(0, 0, 500, 620));
UIImage* part = [UIImage imageWithCGImage:cgImg];
UIImageView* imageView = [[UIImageView alloc] initWithImage:part];
imageView.tag = BACKGROUNDVIEWTAG;
[self.view insertSubview:imageView atIndex:0];
}
Related
I have a images sequence animated with :
UIImageView *imgView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 120)];
[UIImage animatedImageNamed:#"spinner-" duration:1.0f];
imgView.contentMode = UIViewContentModeCenter;
imgView.animationRepeatCount = 1; // No effect
[self.view addSubView:imgView];
But the sequence keeps looping. Is there a way to control the repeat count (and keep displaying the last image) ?
In your case, using [UIImage animatedImageNamed:#"spinner-" duration:1.0f]; won't give you what you want.
For what you would like to achieve, I would recommend using UIImageView.
Usage:
First create an UIImageView object:
UIImageView *imageView = [[UIImageView alloc] initWithFrame:<FRAME_SIZE>];
Second, create an array that holds your images to be animated.
If you just have a few, do the following:
NSMutableArray *animatedImagesArray = [[NSMutableArray alloc] initWithObjects:[UIImage imageNamed:#"spinner-1.png"], [UIImage imageNamed:#"spinner-2.png"], nil];
If you have more, then you could load your images into the array like this:
for (int i = 0; i < <NUM_OF_IMAGE_COUNT>; i++)
{
[animatedImagesArray addObject:[UIImage imageNamed:[NSString stringWithFormat:#"spinner-%d%#", i, #".png"]]];
}
Once all that is done, then we configure the controls you want:
imageView.animationImages = animatedImagesArray;
imageView.animationDuration = 1.0f;
imageView.animationRepeatCount = 1.0f;
Then start animating it by:
[imageView startAnimating];
And stop the animation via:
[imageView stopAnimating];
If you would like the UIImageView to hold the last image after stopping, we would need to carry out the following before starting the animation:
imageView.image = [imageView.animationImages lastObject];
....
[imageView startAnimating];
Hope this helps.
You set the repeat count in the UIImageView object that displays your image.
UIImage *testImage = [UIImage animatedImageNamed:#"Box.jpg" duration:1.0f];
UIImageView *imgView = [[UIImageView alloc] initWithImage:testImage];
[imgView setAnimationRepeatCount:1];
In our iOS app, we want a loading image which is just our logo, animated. I'm wondering what the best practice is to do this? Would it be to use a series of .png images, or one long .png image. I can also thing of using a .gif but the colour quality is not the greatest.
It may also be possible, since the animation is just resizing of certain elements, to do the animation programatically using several UIimages and resizing them, though it may not be as smooth.
UIImage* img1 = [UIImage imageNamed:#"image_1.png"];
UIImage* img2 = [UIImage imageNamed:#"image_2.png"];
UIImage* img3 = [UIImage imageNamed:#"image_3.png"];
UIImage* img4 = [UIImage imageNamed:#"image_4.png"];
UIImage* img5 = [UIImage imageNamed:#"image_5.png"];
NSArray *images = [NSArray arrayWithObjects:img1,img2,img3,img4,img5,nil];
UIImageView* imageView = [[UIImageView alloc]
initWithFrame:CGRectMake((self.view.frame.size.width-80)/2, 50, 80.0, 80.0)];
[imageView setAnimationImages:images];
[imageView setAnimationRepeatCount:100];
[imageView setAnimationDuration:3.0];
[imageView startAnimating];
imageView.tag = 100;
[self.view addSubview:imageView];
Currently I'm trying to set a title image for my UINavigationBar, but it's stretching quite wide. I'm using the following code:
CGRect myImageRect = CGRectMake(0.0f, 0.0f, 40.0f, 20.0f);
UIImageView *myImage = [[UIImageView alloc] initWithFrame:myImageRect];
[myImage setImage:[UIImage imageNamed:#"titleImage.png"]];
self.navigationItem.titleView = myImage;
Does anybody have an idea as to why it's stretching so much width wise?
can you try setting the contentMode on either the navigationItem or the UIImageView?
myImage.contentMode = UIViewContentModeAspectFill;
Simply use following code it will set image to center of navigationbar..
UIImage *image = [UIImage imageNamed:#"sclogo.png"];
self.navigationItem.titleView = [[[UIImageView alloc] initWithImage:image] autorelease];
If you want to set image in full navigation bar than you can use follwoing code :-
UINavigationBar *navBar = self.navigationController.navigationBar;
UIImage *image = [UIImage imageNamed:#"youimage"];
[navBar setBackgroundImage:image forBarMetrics:UIBarMetricsDefault];
You are trying to set image in navigation bar title view.
Try this:
imageView.contentMode = UIViewContentModeScaleAspectFit;
I am trying to center a UIImageView in a scroll view. Here is the code I tried with no results.
UIImageView *iv = [[UIImageView alloc] initWithImage:[UIImage imageNamed:kReceiptDetailScrollViewBackgroundCornered]];
[iv setContentMode:UIViewContentModeCenter];
[[self scrollView] insertSubview:iv atIndex:0];
Anybody know what's wrong with this?
You set the image view's content mode to center, which means that the image is not scaled and it can be larger than the image view that displays it.
UIImage *image = [UIImage imageNamed: kReceiptDetailScrollViewBackgroundCornered];
UIImageView *iv = [[UIImageView alloc] initWithImage: image];
iv.contentMode = UIViewContentModeScaleAspectFit;
iv.frame = [self scrollView].bounds;
[[self scrollView] addSubview: iv];
In my app, i have a little problem in memory issue.
My coding skill is not enough to perfect.
My code use memory four time that i really need.
How can i change my coding?
My Code is
-(void)viewDidAppear:(BOOL)animated
{
UIScrollView * ScrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 44, self.view.frame.size.width, self.view.frame.size.height-88)];// 44(UpperNavigtionBar),-88(Upper+Lower)
ScrollView.pagingEnabled = YES;
// Create a UIImage to hold Info.png
UIImage *image1 = [UIImage imageNamed:#"Image-001.jpg"];
UIImage *image2 = [UIImage imageNamed:#"Image-002.jpg"];
UIImage *image3 = [UIImage imageNamed:#"Image-003.jpg"];
UIImage *image4 = [UIImage imageNamed:#"Image-004.jpg"];
UIImage *image5 = [UIImage imageNamed:#"Image-005.jpg"];
UIImage *image6 = [UIImage imageNamed:#"Image-006.jpg"];
UIImage *image7 = [UIImage imageNamed:#"Image-007.jpg"];
UIImage *image8 = [UIImage imageNamed:#"Image-008.jpg"];
UIImage *image9 = [UIImage imageNamed:#"Image-009.jpg"];
UIImage *image10 = [UIImage imageNamed:#"Image-010.jpg"];
UIImage *image11 = [UIImage imageNamed:#"Image-011.jpg"];
UIImage *image12 = [UIImage imageNamed:#"Image-012.jpg"];
UIImage *image13 = [UIImage imageNamed:#"Image-013.jpg"];
NSArray *images = [[NSArray alloc] initWithObjects:image1,image2,image3,image4,image5,image6,image7,image8,image9,image10,image11,image12,image13,nil];
NSInteger numberOfViews = 13;
for (int i = 0; i < numberOfViews; i++)
{
CGFloat xOrigin = i * self.view.frame.size.width;
UIImageView * ImageView = [[UIImageView alloc] initWithFrame:CGRectMake(xOrigin, 0, self.view.frame.size.width, self.view.frame.size.height-88)]; // -88(Upper+Lower)
[ImageView setImage:[images objectAtIndex:i]];
[ScrollView addSubview:ImageView];
}
ScrollView.contentSize = CGSizeMake(self.view.frame.size.width*numberOfViews, self.view.frame.size.height-88); // -88(for adding Image View as subview)
[self.view addSubview:ScrollView];
}
There are several things you can do to limit your memory usage. First, you should fill your array with the names of the images, not the images themselves. Given the way your images are named, you should be able to do this in a loop. Second, when you load the image, use imageWithContentsOfFile: instead of imageNamed:. The latter method caches images, while the former doesn't. If you use imageNamed: the memory usage will continue to grow as you scroll through images (even with lazy loading) because of the caching -- at some point the system should purge the cache, which should keep your app from crashing, but I don't think it will keep the system for shutting down other programs that are in the background on a device.