iOS開發-簡單的圖片查看器

來源:互聯網
上載者:User

標籤:

現在你只要拿著手機,不管你Android還是iOS,新聞類的App不可避免都有一個功能就是圖片查看,做個專題,查看一下內容,App Store中也有專門針對圖片瀏覽的App,鑒於目前所知有限,無法做到那麼高大上的App,簡單的做個美女查看的Demo。沒有太多的功能,上一張,下一張,標籤,圖片,簡簡單的,深刻的感覺到知識就是力量,目前知識有限的結果就是Demo簡單,且學且珍惜吧。

1.建立項目(如果不會可以參考本人之前的文章),然後在StoryBoard中進行布局,將Girl檔案夾中的圖片拖入項目中;

2.將UIImageView,UILabel,UIButton拖入StoryBoard中,並且設定背景圖片;

設定背景圖片:

3.ViewController.h中定義成員變數:

#import <UIKit/UIKit.h>@interface ViewController : UIViewController@property (weak, nonatomic) IBOutlet UIImageView *imageView;@property (weak, nonatomic) IBOutlet UILabel *pageLabel;@end

4.上一張和下一張的事件代碼:

定義一個圖片數組:

@interface ViewController ()@property NSArray *imageArr;@end@implementation ViewController- (void)viewDidLoad {    [super viewDidLoad];    // Do any additional setup after loading the view, typically from a nib.     [email protected][@"girl0.jpg",@"girl1.jpg",@"girl2.jpg"];}

上一張的代碼:

//顯示上一張- (IBAction)preview:(id)sender {    NSInteger currentIndex=[[[_pageLabel text] substringToIndex:1] integerValue];    NSInteger allCount=[_imageArr count];       if (currentIndex==1) {        currentIndex=allCount;    }else{        currentIndex=currentIndex-1;    }    //設定標籤    [_pageLabel setText:[NSString stringWithFormat:@"%ld/%ld",currentIndex,(long)allCount]];    //擷取圖片的名稱    NSString *imageName=[NSString stringWithFormat:@"girl%ld.jpg",(long)currentIndex-1];        UIImage *image=[UIImage imageNamed:imageName];    [_imageView setImage:image];    }

 下一張代碼:

//顯示下一張- (IBAction)nextView:(id)sender {    //截取標籤上面的數字    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];    }

以上的代碼基本上都是OC基礎,UIImage設定圖片的時候只需要傳遞一片名稱即可,不需要傳遞路徑;

 5.最終效果如下:

效果很簡單,就是在上一張和下一張到臨界點的時候判斷一下,兩者的代碼類似,其實可以封裝一下,周末愉快;

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];    // Do any 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 that can be recreated.}//顯示上一張- (IBAction)preview:(id)sender {    NSInteger currentIndex=[[[_pageLabel text] substringToIndex:1] integerValue];    NSInteger allCount=[_imageArr count];       if (currentIndex==1) {        currentIndex=allCount;    }else{        currentIndex=currentIndex-1;    }    //設定標籤    [_pageLabel setText:[NSString stringWithFormat:@"%ld/%ld",currentIndex,(long)allCount]];    //擷取圖片的名稱    NSString *imageName=[NSString stringWithFormat:@"girl%ld.jpg",(long)currentIndex-1];        UIImage *image=[UIImage imageNamed:imageName];    [_imageView setImage:image];    }//顯示下一張- (IBAction)nextView:(id)sender {    //截取標籤上面的數字    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開發-簡單的圖片查看器

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.