// 设备的图片库 所有同步到iphone的图像 以及包括用户拍摄的图片在内的任何相册
//UIImagePickerControllerSourceTypeSavedPhotosAlbum 仅含相册
//UIImagePickerControllerSourceTypeCamera 允许用户使用iPhone内置的摄像头拍照
#define SOURCETYPE UIImagePickerControllerSourceTypePhotoLibrary
#define DOCSFOLDER [NSHomeDirectory() stringByAppendingPathComponent:@"Documents"]
- (id) init
{
if (!(self = [super init])) return self;
// Attempt to use a Photo Library browser 尝试使用一个图片库浏览器
if ([UIImagePickerController isSourceTypeAvailable:SOURCETYPE])
self.sourceType = SOURCETYPE;//isSourceTypeAvailable 检查制定源是否可用
self.delegate = self;
return self;
}
//下面的这个函数是将文件保存到文件夹里
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingImage:
(UIImage *)image editingInfo:(NSDictionary *)editingInfo
{
//找到一个独特的路径名称,一个空的文件
//stringByAppendingPathComponent 从顶层文件夹中加载一个名为selectedImage.png 的文件
int i = 0;
NSString *uniquePath = [DOCSFOLDER stringByAppendingPathComponent:@"selectedImage.png"];
while ([[NSFileManager defaultManager] fileExistsAtPath:uniquePath])
uniquePath = [NSString stringWithFormat:@"%@/%@-%d.%@", DOCSFOLDER,
@"selectedImage", ++i, @"png"];
printf("Writing selected image to Documents folder\n");
[UIImagePNGRepresentation(image) writeToFile: uniquePath atomically:YES];
//UIImageJPEGRepresentation 带有两个参数 图像和范围0.0(最大压缩)到1.0(最小压缩)
//而UIImagePNGRepresentation后面仅代有一个图像
//要将一个图片写到一个文件上用writeToFile:atomically:这可将图片存出到指定的目录下将第二个参数
//设为YES确保整个文件夹在被放到目录中之前获得写操作 保证你不必处理部分写操作带来的后果
//每个UIImage都可将自身转化为JPEG后PNG数据 这两个内置UIKit函数从UIImage实例生成必需的NSData
[self popToRootViewControllerAnimated:YES];
//[self setNavigationBarHidden:YES animated:YES];
// 刚开始的时候显示 点击图片返回后没有了导航栏
/*pushViewController:viewController animated:BOOL
(加载视图控制器)– 添加指定的视图控制器并予以显示,后接:是否动画显示
popViewControllerAnimated:BOOL
(弹出当前视图控制器)– 弹出并向左显示前一个视图
popToViewController:viewController animated:BOOL
(弹出到指定视图控制器)– 回到指定视图控制器, 也就是不只弹出一个
popToRootViewControllerAnimated:BOOL
(弹出到根视图控制器)– 比如说你有一个“Home”键,也许就会实施这个方法了.回到根视图控制器那里
setNavigationBarHidden:BOOL animated:BOOL
(设置导航栏是否显示)– 如果你想隐藏导航栏,这就是地方了。参照Picasa的WebApp样式*/
}
//点击cancel健时 要引发的事件
- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker
{
printf("User cancelled\n");
[self popToRootViewControllerAnimated:YES];
}
如果把上面两个函数中的的[self popToRootViewControllerAnimated:YES]; 改为[[self parentViewController]
dismissModalViewControllerAnimated:YES]; 则用户就可以选择编辑图像啦 再有下面的这两个函数
- (void) snap
{
[self presentModalViewController:[[HelloController alloc] init] animated:YES];
}
- (void) loadView
{
[super loadView];
UIView *contentView = [[UIView alloc] initWithFrame:[[UIScreen mainScreen] applicationFrame]];
contentView.backgroundColor = [UIColor whiteColor];
self.view = contentView;
[contentView release];
self.navigationItem.rightBarButtonItem = [[[UIBarButtonItem alloc]
initWithTitle:@"Select"
style:UIBarButtonItemStylePlain
target:self
action:@selector(snap)] autorelease];
self.title = @"Image Picker";
}