iOS7 custom loading screen. PNG sequence, GIF, or animated UIimages - ios

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];

Related

How to set an image into an UIImageView without any quality loss and getting the average colour of the image

I want to do something like this. Im selecting maximum 8 images from my phone photo gallery. And I set it into several image views. Those image views are same size. But my images are different sizes.So some images looks so ugly because of stretching.
So How can I set the image inside a UIImageView without any quality loss.
Also How can I set the UIImageview bckground colour as the average colour of the current image which inside of that UIImageView
You can use the contentMode property to scale your image accordingly.
For example,
image.contentMode = UIViewContentModeScaleToFill;
This will scale your image to the size of your UIImageView frame.
If you're loading images in from a server, you will have to load the data according to UIImageJPEGRepresentation. That takes two parameters, the image and quality.
For example,
NSData *data = UIImageJPEGRepresentation(image, 1.0);
Where 1.0 is the highest quality :)
Good luck!
Use UIViewContentModeScaleAspectFit like this...
UIImageView *imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"myimage.png"]];
[imageView setContentMode:UIViewContentModeScaleAspectFit];
[self.view addSubview:imageView];
And your image name should be image#2x.png if possible to get the original quality
UPDATE
If you are getting image from the image picker, use this..
-(void) imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {
[picker dismissViewControllerAnimated:YES completion:nil];
UIImage * pickedImage = [info objectForKey:UIImagePickerControllerOriginalImage];
UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 320, 200)];
[imageView setContentMode:UIViewContentModeScaleAspectFit];
[imageView setImage:pickedImage];
[self.view addSubview:imageView];
}
imageView frame 320x200 pixels with UIViewContentModeScaleAspectFill
imageView frame 320x200 pixels with UIViewContentModeScaleAspectFilt

colorWithPatternImage vs CGImage vs UIImageView

I've seen on many forums that you shouldn't use colorWithPatternImage for memory issues, you should use UIImageView if you are using a large background image and add as a subview to save memory. I tried with these three solutions and all of those are showing same memory used:
// First Option
UIImageView *backgroundView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"Background"]];
[self.view addSubview:backgroundView];
[self.view sendSubviewToBack:backgroundView];
//Second Option
UIImage *image = [UIImage imageNamed:#"Background"];
self.view.layer.contents = (id) image.CGImage;
//Third Option
self.view.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:#"Background"]];
I am using iOS7 SDK. Is there anything I am missing or have iOS7 improved in this direction?

Centering a UIImageView inside a UIScrollView

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];

How can i add image names into an array?

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.

IOS Animated UINavigationController Global Background Image

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];
}

Resources