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


現在你只要拿着手機,不管你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.
     _imageArr=@[@"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.
     _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


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM