iOS Programming Camera  2 

1.1 Creating BNRImageStore

The image store will fetch and cache the images as they are needed. It will also be able to flush the cache if the device runs low on memory.

 

Create a new NSObject subclass called BNRImageStore. Open BNRImageStore.h and create its interface:

 

#import <Foundation/Foundation.h> @interface BNRImageStore : NSObject + (instancetype)sharedStore;

- (void)setImage:(UIImage *)image forKey:(NSString *)key; - (UIImage *)imageForKey:(NSString *)key;
- (void)deleteImageForKey:(NSString *)key;

@end

 

In BNRImageStore.m, add a class extension to declare a property to hang onto the images. @interface BNRImageStore ()

@property (nonatomic, strong) NSMutableDictionary *dictionary; @end
@implementation BNRImageStore

 

Like the BNRItemStore, the BNRImageStore needs to be a singleton. In BNRImageStore.m, write the following code to ensure BNRImageStore's singleton status.

@implementation BNRImageStore

+ (instancetype)sharedStore

{
static BNRImageStore *sharedStore = nil;

if (!sharedStore) {

sharedStore = [[self alloc] initPrivate]; }

return sharedStore; }

// No one should call init - (instancetype)init

{
@throw [NSException exceptionWithName:@"Singleton"

return nil; }

reason:@"Use +[BNRImageStore sharedStore]" userInfo:nil];

// Secret designated initializer - (instancetype)initPrivate

{
self = [super init];

if (self) {

_dictionary = [[NSMutableDictionary alloc] init]; }

return self; }

-  (void)setImage:(UIImage *)image forKey:(NSString *)key
{
[self.dictionary setObject:image forKey:key];

-  (UIImage *)imageForKey:(NSString *)key
{
return [self.dictionary objectForKey:key];

-  (void)deleteImageForKey:(NSString *)key
{
if (!key) {
return; }
[self.dictionary removeObjectForKey:key]; } 

 

1.2 Creating and Using Keys

When an image is added to the store, it will be put into a dictionary under a unique key, and the associated BNRItem object will be given that key. When the BNRDetailViewController wants an image from the store, it will ask its item for the key and search the dictionary for the image. Add a property to BNRItem.h to store the key.

@property (nonatomic, copy) NSString *itemKey;

 

The image keys need to be unique in order for your dictionary to work.

为了让你的dictionary 工作,你必须让image key 是独特的。

While there are many ways to hack together a unique string, you are going to use the Cocoa Touch mechanism for creating universally unique identifiers (UUIDs), also known as globally unique identifiers (GUIDs).

有许多方式组成一个独特的string,你可以使用cocoa touch mechanism 来创建universally unique  identifiers(UUIDs),也就是globally unique identifiers (GUIDs). 

Objects of type NSUUID represent a UUID and are generated using the time, a counter, and a hardware identifier, which is usually the MAC address of the WiFi card.

类型为NSUUID 的对象代表了一个UUID 。它由time,counter,a hardware identifier ,which is usually the mac address of the  wifi card .

When represented as a string, UUIDs look something like this:

4A73B5D2-A6F4-4B40-9F82-EA1E34C1DC04

 

#import "BNRImageStore.h"

 

// Create an NSUUID object - and get its string representation NSUUID *uuid = [[NSUUID alloc] init];
NSString *key = [uuid UUIDString];

_itemKey = key;

 

 

// Store the image in the BNRImageStore for this key [[BNRImageStore sharedStore] setImage:image

forKey:self.item.itemKey];

 

Therefore, the key is a persistent way of referring to an image.

一个key 是指向一个image 的持久化方式。

1.3 Wrapping up BNRImageStore

you need to teach BNRDetailViewController how to grab the image for the selected BNRItem and place it in its imageView.

你应该告诉BNRDetailViewController怎样获取image 对于选定的BNRItem,并把它放到它的imageView上。

 

The BNRDetailViewController's view will appear at two times: when the user taps a row in BNRItemsViewController and when the UIImagePickerController is dismissed. In both of these situations, the imageView should be populated with the image of the BNRItem being displayed.

NSString *imageKey = self.item.imageKey;

// Get the image for its image key from the image store

UIImage *imageToDisplay = [[BNRImageStore sharedStore] imageForKey:imageKey];

// Use that image to put on the screen in the imageView

self.imageView.image = imageToDisplay;

 

If there is no image associated with the item, then imageForKey: will return nil. When the image is nil, the UIImageView will not display an image.

如果没有与该项连接的image,那么imageForKey 将会返回nil。当image 是nil时,UIImageView将不展示一个image。

1.4 Dismissing the Keyboard

 

When the keyboard appears on the screen in the item detail view, it obscures BNRDetailViewController's imageView. This is annoying when you are trying to see an image, so you are going to implement the delegate method textFieldShouldReturn: to have the text field resign its first responder status to dismiss the keyboard when the return key is tapped.

所以你将要实现委托 方法textFieldShouldReturn 来让text field resign 它的first responder status 来取消keyboard 当return 建按下时候。 

- (BOOL)textFieldShouldReturn:(UITextField *)textField

{
[textField resignFirstResponder];

return YES; }

 

It would be stylish to also dismiss the keyboard if the user taps anywhere else on BNRDetailViewController's view. You can dismiss the keyboard by sending the view the message endEditing:, which will cause the text field (as a subview of the view) to resign as first responder.

你可以通过给view发 信息endEditing来解除keyboard,这将会导致text field  resign as first responder .

You have seen how classes like UIButton can send an action message to a target when tapped. Buttons inherit this target-action behavior from their superclass, UIControl.

Buttons 继承这个tareget action  行自 UIControl。

You are going to change the view of BNRDetailViewController from an instance of UIView to an instance of UIControl so that it can handle touch events.

 

In BNRDetailViewController.xib, select the main View object. Open the identity inspector and change the view's class to UIControl

- (IBAction)backgroundTapped:(id)sender

{
[self.view endEditing:YES];

}

 

 

iOS Programming Camera 2的更多相关文章

  1. iOS Programming Camera 1

     iOS Programming Camera  1 1 Displaying Images and UIImageView 1.1  put an instance of UIImageView o ...

  2. iOS Programming Autorotation, Popover Controllers, and Modal View Controllers

    iOS Programming Autorotation, Popover Controllers, and Modal View Controllers  自动旋转,Popover 控制器,Moda ...

  3. iOS Programming State Restoration 状态存储

    iOS Programming State Restoration 状态存储 If iOS ever needs more memory and your application is in the ...

  4. Head First iOS Programming

    内部分享: Head First iOS Programming http://www.slideshare.net/tedzhaoxa/head-first-ios-programming-4606 ...

  5. iOS Programming Recipe 6: Creating a custom UIView using a Nib

    iOS Programming Recipe 6: Creating a custom UIView using a Nib JANUARY 7, 2013 BY MIKETT 12 COMMENTS ...

  6. iOS Programming Controlling Animations 动画

    iOS Programming Controlling Animations 动画 The word "animation" is derived from a Latin wor ...

  7. iOS Programming UIStoryboard 故事板

    iOS Programming UIStoryboard In this chapter, you will use a storyboard instead. Storyboards are a f ...

  8. iOS Programming NSUserDefaults

    iOS Programming NSUserDefaults  When you start an app for the first time, it uses its factory settin ...

  9. iOS Programming Localization 本地化

    iOS Programming Localization 本地化 Internationalization is making sure your native cultural informatio ...

随机推荐

  1. 【iOS系列】-autorelease的作用

    内存管理原则(配对原则):只要出现了new,alloc,retain方法,就要配对出现release,autorelease   1:对象存入到自动释放池中,当这个池子被销毁的时候他会对池子中所有的对 ...

  2. 深入研究java.lang.Object类

    前言:Java的类库日益庞大.所包括的类和接口也不计其数.但当中有一些非常重要的类和接口,是Java类库中的核心部分.常见的有String.Object.Class.Collection.ClassL ...

  3. [Unity3D]Unity3D游戏开发之连续滚动背景

    在诸如天天跑酷等2D游戏中.因为游戏须要表现出运动的感觉.通常都会使游戏背景连续循环滚动以增强视觉效果,那么今天.博主就来带领大家一起来实现连续滚动背景吧. 首先来讲述一下原理.准备两张连续的图片(博 ...

  4. 记录下docker的常用命令

    常用命令: docker images:查看本地所有镜像 docker pull  centos:7:从仓库中获取镜像 docker ps:列出所有正在运行的容器 docker ps -a:列出所有容 ...

  5. YTU 2411: 谁去参加竞赛?【简单循环】

    2411: 谁去参加竞赛?[简单循环] 时间限制: 1 Sec  内存限制: 64 MB 提交: 461  解决: 261 题目描述 学校要举办大学生程序设计竞赛,老师要求期末考试成绩在平均成绩以上的 ...

  6. jfreechart应用3--饼状图 学习(作者:百度 被风吹过的日子)

    jfreechart应用3--饼状图 三. 饼图 在WebRoot目录下建立名为pie的子目录,用来存放本教程中饼图的实例jsp页面.下面让我们来看一个简单的三维饼图.首先在pie目录下建立一个名为s ...

  7. 并不对劲的bzoj4816:loj2000:p3704[SDOI2017]数字表格

    题目大意 有函数\(f(x)\),\(f(0)=0,f(1)=1,f(x)=f(x-1)+f(x-2)\) \(t\)(\(t\leq1000\))组询问,每次给定\(n,m\)(\(n,m\leq1 ...

  8. 【转】Android 6.0 Marsmallow BLE : Connection Parameters

    原文网址:http://stackoverflow.com/questions/34617061/android-6-0-marsmallow-ble-connection-parameters Th ...

  9. 虚拟机C盘扩容

    使用 <分区助手> 下载地址:http://115.com/file/belj8wkm

  10. NOIp2013 车站分级 【拓扑排序】By cellur925

    题目传送门 我们注意到,题目中说:如果这趟车次停靠了火车站 x,则始发站.终点站之间所有级别大于等于火车站x的都必须停靠.有阶级关系,满满的拓扑排序氛围.但是,如果我们按大于等于的关系连,等于的情况就 ...