UIImageView - BNR

时间:2023-03-08 16:38:09
UIImageView - BNR

  继续上节UINavigationController - BNR

  打开BNRDetailViewController.xib文件,向view中添加UIImageView对象,选中该对象,通过Attributes Inspector -> View -> Mode -> Aspect Fit,调整UIImageView对象的contentMode属性。

  同时,添加UIToolBar拖到view的底部。选中UIToolBar的Item,通过Attributes Inspector -> Bar Button Item -> System Item -> Camera,将其改为相机图标。此相机按钮需要一个target和action。如下所示:

UIImageView - BNR

  在BNRDetailViewController.m的类扩展中与UIImageView建立outlet连接,如下:

UIImageView - BNR

  选择相机按钮,Control-Drag到BNRDetailViewController.m,创建takePicture:方法,如下:

UIImageView - BNR

  同时,将UIToolBar建立一个outlet,取名为toolBar。


  当我们创建了一个UIImagePickerController对象时,必须设置它的sourceType属性和给它分配一个delegate。

sourceType常量告诉UIImagePicker对象从哪里获取照片。其有三个可选的值,1)UIImagePickerControllerSourceTypeCamera,拍摄一张新的照片,使用该常量前,需要将isSourceTypeAvailable:方法发送给UIImagePickerController,检验该设备是否有相机功能;2)UIImagePickerControllerSourceTypePhotoLibrary,将选择一个照片集,从该照片集中选择照片;3)UIImagePickerControllerSourceTypeSavedPhotoAlbum,将从最近拍摄的照片中获取。

  在BNRDetailViewController.m中,添加协议:

@interface BNRDetailViewController () <UINavigationControllerDelegate, UIImagePickerControllerDelegate>

  在BNRDetailViewController.m中,修改takePicture:方法如下:

 - (IBAction)takePicture:(id)sender {
UIImagePickerController *imagePicker = [[UIImagePickerController alloc] init];
if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) {
imagePicker.sourceType = UIImagePickerControllerSourceTypeCamera;
} else {
imagePicker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
}
imagePicker.delegate = self;
[self presentViewController:imagePicker animated:YES completion:nil];
}

  UIImagePickerController对象已模态方式呈现。一个模态视图控制器将占据整个屏幕直到结束操作。

  当用户已经选好一张照片时,imagePickerController:didFinishPickingMediaWithInfo:方法给被自动发送给UIImagePicker的委托对象。

 - (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary<NSString *,id> *)info {
// 从info字典中获取选择的image
UIImage *image = info[UIImagePickerControllerOriginalImage];
self.imageVeiw.image = image;
// 退出UIImagePickerController
[self dismissViewControllerAnimated:YES completion:nil];
}

  将照片存入disk中,当需要它们的时候才放到RAM中。

  创建一个新的NSObject的子类BNRImageStore,其将持有用户使用的照片,其为单例模式。因为图像一般都比较大,将它们与其它数据分离开比较少。

  打开BNRImageStore.h,添加如下方法:

#import <UIKit/UIKit.h>  //此处将Foundation/Foundation.h修改为UIKit/UIKit.h

@interface BNRImageStore : NSObject

+ (instancetype)sharedImage;

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

  打开BNRImageStore.m文件,添加类扩展,并声明一个变量,如下:

 @interface BNRImageStore ()

 @property (nonatomic, strong) NSMutableArray *dictionary;  // 存放图片

 @end

  再添加下面的代码:

 + (instancetype)sharedImage {
static BNRImageStore *shareStore = nil;
if (!shareStore) {
shareStore = [[BNRImageStore alloc] initPrivate];
}
return shareStore;
} - (instancetype)init {
@throw [NSException exceptionWithName:@"Singleton"
reason:@"Use+[BNRImageStore sharedStore]"
userInfo:nil];
return nil;
} // Designated initializer
- (instancetype)initPrivate {
self = [super init];
if (self) {
_dictionary = [[NSMutableDictionary alloc] init];
}
return self;
}

  实现在头文件中声明的三个实例方法:

 - (void)setImage:(UIImage *)image forKey:(NSString *)key {
self.dictionary[key] = image;
} - (UIImage *)imageForKey:(NSString *)key {
return self.dictionary[key];
} - (void)deleteImageForKey:(NSString *)key {
if (!key) {
return;
}
[self.dictionary removeObjectForKey:key];
}

  当你需要一个模型对象时,需要创建一个继承自NSObject的新类,并且给予它恰当的实例变量。

  在BNRItem.h中,添加一个实例变量用来存放图片的键值,如下:

 @property (nonatomic, copy) NSString *itemKey;  // 存放BNRImageStore中图像的键值

  同时打开BNRItem.m文件,导入BNRImageStore头文件,如下:

 #import "BNRImageStore.h"

  我们将使用通用唯一标识码(Universally Unique Identifier,UUIDs)作为key值。在Objective-C中,用NSUUID类来代表UUID。修改BNRItem.m中的initWithItemName:valueInDollars:serialNumber:方法如下:

     // designated initializer
- (instancetype)initWithItemName:(NSString *)name
valueInDollars:(int)value
serialNumber:(NSString *)sNumber
{
// Call the superclass's designated initializer
self = [super init]; // Did the superclass's designated initializer succeed?
if (self) {
// Give the instance variables initial values
_itemName = name;
_serialNumber = sNumber;
_valueInDollars = value;
// Set _dateCreated to the current date and time
_dateCreated = [[NSDate alloc] init]; NSUUID *uuid = [[NSUUID alloc] init];
NSString *key = [uuid UUIDString];
_itemKey = key;
} // Return the address of the newly initialized object
return self;
}

  修改BNRDetailViewController.m中的imagePickerController:didFinishPickingMediaWithInfo:方法如下:

 - (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary<NSString *,id> *)info {
// 从info字典中获取选择的image
UIImage *image = info[UIImagePickerControllerOriginalImage];
[[BNRImageStore sharedImage] setImage:image forKey:self.item.itemKey];

self.imageVeiw.image = image;
// 退出UIImagePickerController
[self dismissViewControllerAnimated:YES completion:nil];
}

当用户每次获取到图片的时候,就会将其存入image store中。BNRImageStore和BNRItem类都知道图像的key,因此能通过两者来获取图片。

  打开BNRItemStore.m,导入BNRImageStore的头文件,如下:

#import "BNRImageStore.h"

  当删除一个item的时候,需要从image store 中删除其图像,修改removeItem:方法如下:

 - (void)removeItem:(BNRItem *)item {
NSString *key = item.itemKey;
[[BNRImageStore sharedImage] deleteImageForKey:key];
[self.privateItems removeObjectIdenticalTo:item];
}

  当图片第一次被创建的时候,它将存在于内存中特定的地址中。下一次程序启动的时候,图片在内存中的地址将会发生改变,因此不能通过指针来获取图片。

  打开,BNRDetailViewController.m,在类扩展中添加UITextFieldDelegate协议,然后修改textFieldShouldReturn:方法,这样当输入结束时,点击return按钮可隐藏键盘。代码如下:

 - (BOOL)textFieldShouldReturn:(UITextField *)textField {
[textField resignFirstResponder];
return YES;
}

  

  为了让用户点击屏幕上的任何地方都能隐藏键盘,首先,打开BNRDetailViewController.xib,选中view视图,打开Attributes Inspector,将其class改为UIControl。

  按住view,Control-drag到BNRDetailViewController.m文件的实现处,按下图构造一个action:

UIImageView - BNR

  修改刚添加的action代码如下:

 - (IBAction)backgroundTapped:(id)sender {
[self.view endEditing:YES];
}

程序代码如下:http://pan.baidu.com/s/1c00muxa


  UIImagePickerController对象有一个mediaType属性,其为字符串数组,包含标识符,来说明可以选择哪种类型的media。默认只包含kUTTypeImage,可向其添加kUTTypeMovie,使UIImagePickerController能够选择视频。修改BNRDetailViewController.m的takePicture:方法,

 - (IBAction)takePicture:(id)sender {
UIImagePickerController *imagePicker = [[UIImagePickerController alloc] init];
NSArray *availableType = [UIImagePickerController availableMediaTypesForSourceType:UIImagePickerControllerSourceTypeCamera];
imagePicker.mediaTypes = availableType;

if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) {
imagePicker.sourceType = UIImagePickerControllerSourceTypeCamera;
} else {
imagePicker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
}
imagePicker.delegate = self;
[self presentViewController:imagePicker animated:YES completion:nil];
}

真机测试结果如下:

UIImageView - BNR

次时在模拟器上测试会遇到会遇到NSInvalidArgumentException错误。只能在真机上测试。

如何免费设置真机调试,请参考下述连接的文章:http://www.bubuko.com/infodetail-1061938.html

在真机上运行时,若出现Security的警告,则在iPhone的设置里面:Settings -> General -> Profiles,选择DEVELOPER APP下的开发者账号,之后选择Trust即可。