现在你只要拿着手机,不管你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

iOS开发-简单的图片查看器的更多相关文章

  1. Objective-C ,ios,iphone开发基础:快速实现一个简单的图片查看器

    新建一个single view 工程: 关闭ARC , 在.xib视图文件上拖放一个UIImageView  两个UIButton ,一个UISlider ,布局如图. 并为他们连线, UIImage ...

  2. 基于jQuery的一个简单的图片查看器

    项目中自己diy了一个图片查看器.因为初始代码不是自己的,只是在上面改了一下也没有弄的很漂亮.等以后有时间了在重写一下样式和封装,作为备用的只是积累吧.如果有童鞋有用到,完全可以在此基础上改,比较容易 ...

  3. wpf图片查看器,支持鼠标滚动缩放拖拽

    最近项目需要,要用到一个图片查看器,类似于windows自带的图片查看器那样,鼠标滚动可以缩放,可以拖拽图片,于是就写了这个简单的图片查看器. 前台代码: <Window x:Class=&qu ...

  4. js手写图片查看器(图片的缩放、旋转、拖拽)

    在做一次代码编辑任务中,要查看图片器.在时间允许的条件下,放弃了已经封装好的图片jq插件,现在自己手写js实现图片的缩放.旋转.推拽功能! 具体代码如下: <!DOCTYPE html> ...

  5. js实现图片查看器(图片的缩放、旋转、拖拽)

    一.关于图片查看器. 目前网络上能找到的图片查看器很多,谁便一搜就能出来.如:jquery.iviewer.js.Viewer.js这两个js文件,其中功能也足够满足大部分开发需求.但是单纯的就想实现 ...

  6. 发布两款JQ小插件(图片查看器 + 分类选择器),开源

    图片查看器,github地址:https://github.com/VaJoy/imgViewer 效果如下: 这款当初大概写了2小时,有点匆忙地赶出来的,使用的接口很简单: $.bindViewer ...

  7. linux上的图片查看器FEH_image_view

    Linux上的图片查看器, 简单,没有多余功能,打开快速,体积小 在终端用feh # 直接执行feh显示当前目录所有图片 feh # 或者指定图片名 feh pic1 pic2 pic3 # 显示一个 ...

  8. 网页中的图片查看器viewjs使用

    需求分析: 对于网页中的图片进行连续放大(便于用户清晰查看内容).缩小,旋转等操作,可以使用viewjs图片查看器插件实现. viewjs官方网址:https://github.com/fengyua ...

  9. Android中的图片查看器

    本案例,使用Eclipse来开发Android2.1版本号的图片查看器. 1)首先,打开Eclipse.新建一个Android2.1版本号的项目ShowTu,打开res/values中文件夹下的str ...

随机推荐

  1. 论 ArrayList如何实现线程安全

    一:使用synchronized关键字 二:使用Collections.synchronizedList(); 假如你创建的代码如下:List<Map<String,Object>& ...

  2. 回顾2014 Java发生的5件大事

    回顾2014 Java发生的5件大事 1.2月1日:RedMonk分析师确认并宣布Java是最受欢迎和多样化的语言! 2014年,Java生态圈伴随着引擎的轰鸣起步,随着FOSDEM年会的Free J ...

  3. ceph部署过程中的错误

    ceph版本-jewel 用ssd盘来journal ,格式分区权限问题 [ceph-node2][WARNIN] ceph_disk.main.FilesystemTypeError: Cannot ...

  4. C# 集合类-使用

    关联性集合类 关联性集合类即我们常说的键值对集合,允许我们通过Key来访问和维护集合. 我们来看一下  .net 为我们提供了哪些泛型的关联性集合类: Dictionary<TKey,TValu ...

  5. Where should we fork this repository?

    韩梦飞沙  韩亚飞  313134555@qq.com  yue31313  han_meng_fei_sha 我们应该在哪里分叉这个存储库? Where should we fork this re ...

  6. BZOJ3996 TJOI2015线性代数

    先把矩阵式子化简 原式=∑i=1n∑j=1nA[i]∗B[i][j]∗A[j]−∑i=1nA[i]∗C[i] 因此我们发现问题转化为选取一个点所获收益是B[i][j],代价是C[i][j] 这是一个最 ...

  7. bzoj 1176: [Balkan2007]Mokia&&2683: 简单题 -- cdq分治

    2683: 简单题 Time Limit: 50 Sec  Memory Limit: 128 MB Description 你有一个N*N的棋盘,每个格子内有一个整数,初始时的时候全部为0,现在需要 ...

  8. Git_Bug分支

    软件开发中,bug就像家常便饭一样.有了bug就需要修复,在Git中,由于分支是如此的强大,所以,每个bug都可以通过一个新的临时分支来修复,修复后,合并分支,然后将临时分支删除. 当你接到一个修复一 ...

  9. js异步任务处理方式

    一.es6(es2015)之前:使用原始的callback函数,会陷入回掉地域 this.$http.jsonp('/login', (res) => { this.$http.jsonp('/ ...

  10. Android 手机 ADB FastBoot 命令基本用法

    adb用法: 准备: 1.在电脑上安装相应的USB驱动,在各分区置顶帖子有下载链接 2.手机进入设置->开发人员选项->勾选USB调试 adb devices 查看是否有设备 adb sh ...