Shift Nibble

Happy Coding ...

How to develop image gallery universal app in iOS

It is a simple app which will load and show images already stored within the app. I will keep this example simple so that beginners can learn from it.

I have used scroll view control so that user can swipe through the images to see them. UI Image View controls are added in scroll view using for loop. I have used for loop so that it is easy to change the images any time, in-fact my images had the fixed prefix for all files and number was added after that and no number was missing in series. If this is not the case for you then you will need to have collection with image file names.

Check out the below code for function "loadImages", this function uses for loop to add UI Image Views to Scroll View and also set image file name to each image view as it is added so that images are loaded. Here is the code

- (void)loadImages{

    NSString *fileName = @"pic";
    imageArray = [[NSMutableArray alloc] init];
    NSString *string2;
    for (int i=1; i<=42; i++){
        string2 = [fileName stringByAppendingFormat:@"%d.jpg", i];

        UIImageView *imgView = [[UIImageView alloc] initWithFrame:CGRectMake(width * (i-1), 0, width, height)];
        [imageArray addObject:imgView];
        imgView.image = [UIImage imageNamed: string2];
        imgView.contentMode = UIViewContentModeScaleAspectFit;
        [imgView setBackgroundColor:[UIColor blackColor]];
        scrollView.contentSize = CGSizeMake((i+1) * width, 0);
        [scrollView addSubview:imgView];

First line initialises the string variable with the prefix "Pic" because all of my images has this string in the start, you can change it with yours. In the loop I am adding number with string "Pic" which makes it "Pic1.jpg" for first image as so on.

Next initialising image view object and setting its height and width to the height and width of the screen. Width and height variables contains the width and height of the screen and it does not do it automatically I will share the code to set these variables shortly.

The first parameter is "width * (i-1)", this tells where to place the image view on x axis. It is the offset value, since every image should be placed at the end of previous image I have multiplied the width of the screen with image number. But I have decreased image number by 1 before multiplying because first image "1" should be placed at 0 offset.

Next three lines in-fact loading the image, setting to image view and setting the background colour of image view.

Next I am increasing the width of scroll view because new image view is added, yes you are right I could have done it before starting the loop :). After that adding image view to scroll view.

Below is the code for "viewDidLoad" event method, it is called on load of the view.

- (void)viewDidLoad
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    //Set the height and width variables by screen height and width. Since landscape mode is supported so height is width.
    width = [UIScreen mainScreen].bounds.size.height;
    height = [UIScreen mainScreen].bounds.size.width - 20;
    [self loadImages];

Here I am simply setting the height variable to the width of the screen minus 20 and width variable to the height of the screen. These are done because application is supported in the landscape only, if you want to support both orientations then you would need to inverse them on change of orientation. Minus 20 is done for setting the height excluding the top bar size where battery is shown.

You can make this universal app and same code will work for both because I have set height and width variables based on the screen size so whatever the screen size is code will work. You can add iAds / Goole Ads and put some nice images and upload your app on app store with your name :).

iPhone 5 Launched on 12-Sep-2012

iPhone has launched its iPhone 5, many people around the world were waiting for it. There were many rumor's about it but those has came to an end now. There was a features concept video made for iPhone 5 don’t know if people remember that now or not. Sharing it to recall memories, I will later discuss the specifications and development details for it.

Now the Features

  • It is 18% thinner (7.6 millimeters) than iPhone 4S.
  • It is 20% less in weight than iPhone 4S, iPhone 5 weight is 112 grams.
  • Camera is 8 mega pixels.
  • Body is made of aluminum and glass.
  • It has 4 inch display which makes it 9 millimeters taller than iPhone 4S.
  • Battery has 8 hours talk time and 10 hours video playback time.
  • You can take panoramic view just by moving the camera.
  • iPhone 5 has iOS version 6.
  • It supports Nano SIM which is smaller than the one supported in iPhone 4S.