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

jopen 9年前發布 | 22K 次閱讀 IOS iOS開發 移動開發

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

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

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

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

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

設置背景圖片:

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

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

#import <UIKit/UIKit.h>

@interface ViewController : UIViewController

@property (weak, nonatomic) IBOutlet UIImageView *imageView;

@property (weak, nonatomic) IBOutlet UILabel *pageLabel;

@end</pre></div>

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. _imageArr=@[@"girl0.jpg",@"girl1.jpg",@"girl2.jpg"]; }</pre></div>

    上一張的代碼:

    //顯示上一張
  • (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];

}</pre></div>

下一張代碼:

//顯示下一張

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

}</pre></div>

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

5.最終效果如下:

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

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

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. _imageArr=@[@"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</pre></div>

 本文由用戶 jopen 自行上傳分享,僅供網友學習交流。所有權歸原作者,若您的權利被侵害,請聯系管理員。
 轉載本站原創文章,請注明出處,并保留原始鏈接、圖片水印。
 本站是一個以用戶分享為主的開源技術平臺,歡迎各類分享!