iOS development-A simple picture viewer

Source: Internet
Author: User

Now you just hold the phone, whether you Android or iOS, the news app inevitably has a feature is the picture view, do a project, look at the content, the App Store also has a special image-viewing app, in view of the current limited, unable to do so tall app, Simple to be a beautiful view of the demo. Not too many functions, on one, next, label, picture, simple, profound sense of knowledge is power, the current limited knowledge of the result is the demo simple, and learn and cherish it.

1. Create a new project (if you can not refer to my previous article), and then layout in storyboard, drag the picture in the Girl folder into the project;

2. Drag the Uiimageview,uilabel,uibutton into the storyboard and set the background image;

To set a background image:

Define member variables in 3.viewcontroller.h:

#import <UIKit/UIKit.h> @interface viewcontroller:uiviewcontroller@property (weak, nonatomic) Iboutlet Uiimageview *imageview; @property (weak, nonatomic) Iboutlet UILabel *pagelabel; @end

4. Previous and next event codes:

Define a picture array:

@interface Viewcontroller () @property nsarray *imagearr; @end @implementation viewcontroller-(void) viewdidload {    [ Super Viewdidload];    Additional setup after loading the view, typically from a nib.     [Email protected] [@ "girl0.jpg", @ "girl1.jpg", @ "girl2.jpg"];}

The previous code:

Show previous-(ibaction) preview: (ID) Sender {    nsinteger Currentindex=[[[_pagelabel text] substringtoindex:1] IntegerValue];    Nsinteger Allcount=[_imagearr Count];       if (currentindex==1) {        currentindex=allcount;    } else{        currentindex=currentindex-1;    }    Set Label    [_pagelabel settext:[nsstring stringwithformat:@ "%ld/%ld", Currentindex, (Long) allcount]];    Get the name of the picture    nsstring *imagename=[nsstring stringwithformat:@ "Girl%ld.jpg", (long) currentIndex-1];        UIImage *image=[uiimage Imagenamed:imagename];    [_imageview setimage:image];    }

Next piece of code:

Show Next-(Ibaction) NextView: (ID) Sender {    //intercept the number above the label    Nsinteger Currentindex=[[[_pagelabel text] Substringtoindex:1] IntegerValue];    Nsinteger Allcount=[_imagearr Count];    if (currentindex==allcount) {        currentindex=0;    }    NSString *imagename=[nsstring stringwithformat:@ "Girl%ld.jpg", (long) currentindex];        [_pagelabel settext:[nsstring stringwithformat:@ "%ld/%ld", Currentindex+1, (Long) allcount]];    UIImage *image=[uiimage Imagenamed:imagename];    [_imageview setimage:image];    }

The above code is basically OC Foundation, UIImage set the picture when only need to pass a name can, do not need to pass the path;

5. The final effect is as follows:

The effect is very simple, that is, in the last and next to the critical point of the time to judge, the two code similar, in fact, can be encapsulated, happy weekend;

Code in VIEWCONTROLLER.M:

viewcontroller.m//mypicture////Created by Keso on 15/1/17.//Copyright (c) 2015 Keso. All rights reserved.//#import "ViewController.h" @interface Viewcontroller () @property Nsarray *imagearr;@    End@implementation viewcontroller-(void) viewdidload {[Super viewdidload];     Additional setup after loading the view, typically from a nib. [Email protected] [@ "girl0.jpg", @ "girl1.jpg", @ "girl2.jpg"];}    -(void) didreceivememorywarning {[Super didreceivememorywarning]; Dispose of any resources the can be recreated.}    Show previous-(ibaction) preview: (ID) Sender {nsinteger Currentindex=[[[_pagelabel text] substringtoindex:1] integervalue];       Nsinteger Allcount=[_imagearr Count];    if (currentindex==1) {currentindex=allcount;    }else{currentindex=currentindex-1;    }//Set label [_pagelabel settext:[nsstring stringwithformat:@ "%ld/%ld", Currentindex, (Long) allcount]]; Get the name of the picture nsstring *imagename=[nsstring stringwithformat:@ "girl%ld.jpg", (long) currentIndex-1];    UIImage *image=[uiimage Imagenamed:imagename];    [_imageview Setimage:image];  }//Show Next-(Ibaction) NextView: (ID) Sender {//intercept the number above the tag Nsinteger Currentindex=[[[_pagelabel text] substringtoindex:1]    IntegerValue];    Nsinteger Allcount=[_imagearr Count];    if (currentindex==allcount) {currentindex=0;        } nsstring *imagename=[nsstring stringwithformat:@ "Girl%ld.jpg", (long) currentindex];    [_pagelabel settext:[nsstring stringwithformat:@ "%ld/%ld", Currentindex+1, (Long) allcount]];    UIImage *image=[uiimage Imagenamed:imagename];    [_imageview Setimage:image]; } @end

iOS Development-Simple picture viewer

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.