Objective-C ,ios,iphone開發基礎:快速實現一個簡單的圖片查看器

jopen 12年前發布 | 31K 次閱讀 iOS開發 移動開發 Objective-C

新建一個single view 工程:

d1.png

關閉ARC , 在.xib視圖文件上拖放一個UIImageView  兩個UIButton ,一個UISlider ,布局如圖。

 d2.png

并為他們連線,

UIImageView 和 UISlider 分別定義插座變量,兩個 UIButton 分別 連接兩個Action next和previous ,在為 UISlider 連接一個Action  事件。

d3.png

 

再在.h 文件中聲明兩個實例變量。   NSInteger index ; NSMutableArray* arrayPic ; 一個用來記錄當前圖片的index,一個用來做圖片的容器,

用UISlider 來控制圖片的透明度 (alpha 屬性)。在slider 的界面構建起動作中添加代碼,讓圖片的透明度等于 slider的value。

- (IBAction)sliderChangeValued:(id)sender {

self.imageView.alpha = slider.value;

}</pre>在兩個UIButton 的界面構建起動作中添加代碼,循環遍歷arrayPic中的圖片:</div>

- (IBAction)next:(id)sender {
    if(index == [arrayPic count]){
        index = 0;
    }
    self.imageView.image = [UIImage imageWithContentsOfFile:[arrayPic objectAtIndex:index]];
    index++;
}- (IBAction)previous:(id)sender {
    if(index == -1){
        index = ([arrayPic count] -1);
    }
   self.imageView.image = [UIImage imageWithContentsOfFile:[arrayPic objectAtIndex:index]];
    index--;
}

 

至此:簡單的圖片查看器已經完成,

d4.png

 

整個控制器類中的代碼:

// //  wsqViewController.h//  picLook// //  Created by administrator on 13-9-5.//  Copyright (c) 2013年 MyCompanyName. All rights reserved.// #import <UIKit/UIKit.h>@interface wsqViewController : UIViewController {
    UIImageView imageView;
    UISlider slider;
    NSInteger index ;
    NSMutableArray* arrayPic ;

}

@property (retain, nonatomic) IBOutlet UIImageView imageView; @property (retain, nonatomic) IBOutlet UISlider slider;- (IBAction)sliderChangeValued:(id)sender;- (IBAction)next:(id)sender;- (IBAction)previous:(id)sender;@end</pre></div>

 

 //
//  wsqViewController.m
//  picLook
//
//  Created by administrator on 13-9-5.
//  Copyright (c) 2013年 __MyCompanyName__. All rights reserved.
//

#import "wsqViewController.h"

@implementation wsqViewController
@synthesize imageView;
@synthesize slider;

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Release any cached data, images, etc that aren't in use.
}

#pragma mark - View lifecycle

- (void)viewDidLoad
{
    [super viewDidLoad];
    //獲取mainbudle下所有 jpg格式的圖片,
      arrayPic = [[NSMutableArray alloc ] initWithArray: [[NSBundle mainBundle] pathsForResourcesOfType:@"jpg" inDirectory:nil]];
    index = 0;
    NSLog(@"%@",arrayPic);

}

- (void)viewDidUnload
{
    [self setImageView:nil];
    [self setSlider:nil];
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
}

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
}

- (void)viewDidAppear:(BOOL)animated
{
    [super viewDidAppear:animated];
}

- (void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:animated];
}

- (void)viewDidDisappear:(BOOL)animated
{
    [super viewDidDisappear:animated];
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    // Return YES for supported orientations
    return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown);
}

- (void)dealloc {
    [arrayPic release];
    [imageView release];
    [slider release];
    [super dealloc];
}
- (IBAction)sliderChangeValued:(id)sender {

    self.imageView.alpha = slider.value;
}

- (IBAction)next:(id)sender {
    if(index == [arrayPic count]){
        index = 0;
    }
    self.imageView.image = [UIImage imageWithContentsOfFile:[arrayPic objectAtIndex:index]];
    index++;
}

- (IBAction)previous:(id)sender {
    if(index == -1){
        index = ([arrayPic count] -1);
    }
   self.imageView.image = [UIImage imageWithContentsOfFile:[arrayPic objectAtIndex:index]];
    index--;
}
@end
 本文由用戶 jopen 自行上傳分享,僅供網友學習交流。所有權歸原作者,若您的權利被侵害,請聯系管理員。
 轉載本站原創文章,請注明出處,并保留原始鏈接、圖片水印。
 本站是一個以用戶分享為主的開源技術平臺,歡迎各類分享!