iOS调用系统相机,访问系统相册功能实现
2016-07-05 09:10
417 查看
我们在做应用过程中,难免会遇到要设置用户头像这样的功能,我这里总结了一个调用系统相机,相册的功能实现,写出来与大家分享,如有不足还请指正:
1.我们在调用这个功能的时候,一般都有个用来填充图片的ImageView和点击ImageView触发此方法的事件,这里我就写个ImageView和Button来演示,下面是实现整个功能的代码:
#import "ViewController.h"
#define kWidth [UIScreen mainScreen].bounds.size.width
#define kHeight [UIScreen mainScreen].bounds.size.height
@interface
ViewController ()<UIActionSheetDelegate,UIImagePickerControllerDelegate>
{
BOOL isFullScreen;
}
@property(nonatomic,strong)UIImageView *imageView;
@end
@implementation ViewController
- (void)viewDidLoad {
[superviewDidLoad];
_imageView = [[UIImageViewalloc]initWithFrame:CGRectMake(100,100,
200,
200)];
_imageView.backgroundColor = [UIColorgrayColor];
[self.viewaddSubview:_imageView];
UIButton *chooseBtn = [UIButtonbuttonWithType:UIButtonTypeCustom];
chooseBtn.frame =
CGRectMake(100,
30, 100,
40);
[chooseBtn addTarget:selfaction:@selector(chooseImage:)forControlEvents:UIControlEventTouchUpInside];
[chooseBtn setBackgroundColor:[UIColorbrownColor]];
[chooseBtn setTitle:@"选择照片"forState:UIControlStateNormal];
[self.viewaddSubview:chooseBtn];
}
#pragma mark -
保存图片至沙盒
- (void) saveImage:(UIImage *)currentImage withName:(NSString *)imageName
{
NSData *imageData =
UIImageJPEGRepresentation(currentImage,
0.5);
//
获取沙盒目录
NSString *fullPath = [[NSHomeDirectory()stringByAppendingPathComponent:@"Documents"]stringByAppendingPathComponent:imageName];
//
将图片写入文件
[imageData writeToFile:fullPath
atomically:NO];
}
#pragma mark - image picker delegte
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
[picker dismissViewControllerAnimated:YEScompletion:^{}];
UIImage *image = [infoobjectForKey:UIImagePickerControllerOriginalImage];
[selfsaveImage:imagewithName:@"currentImage.png"];
NSString *fullPath = [[NSHomeDirectory()stringByAppendingPathComponent:@"Documents"]stringByAppendingPathComponent:@"currentImage.png"];
UIImage *savedImage = [[UIImagealloc]
initWithContentsOfFile:fullPath];
isFullScreen =NO;
[self.imageViewsetImage:savedImage];
self.imageView.tag =100;
}
- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker
{
[selfdismissViewControllerAnimated:YEScompletion:^{}];
}
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
isFullScreen = !isFullScreen;
UITouch *touch = [touches
anyObject];
CGPoint touchPoint = [touch
locationInView:self.view];
CGPoint imagePoint =
self.imageView.frame.origin;
//touchPoint.x
,touchPoint.y
就是触点的坐标
//
触点在imageView内,点击imageView时放大,再次点击时缩小
if(imagePoint.x <= touchPoint.x && imagePoint.x +self.imageView.frame.size.width
>=touchPoint.x && imagePoint.y <= touchPoint.y && imagePoint.y+self.imageView.frame.size.height
>= touchPoint.y)
{
//
设置图片放大动画
[UIViewbeginAnimations:nilcontext:nil];
// 动画时间
[UIViewsetAnimationDuration:1];
if (isFullScreen) {
// 放大尺寸
self.imageView.frame =CGRectMake(0,0,
kWidth, kHeight);
}
else {
// 缩小尺寸
self.imageView.frame =CGRectMake(100,100,
200, 200);
}
// commit动画
[UIViewcommitAnimations];
}
}
#pragma mark - actionsheet delegate
-(void) actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex
{
if (actionSheet.tag ==255) {
NSUInteger sourceType =
0;
//
判断是否支持相机
if([UIImagePickerControllerisSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera])
{
switch (buttonIndex) {
case
0:
//
取消
return;
case
1:
//
相机
sourceType =
UIImagePickerControllerSourceTypeCamera;
break;
case
2:
//
相册
sourceType =
UIImagePickerControllerSourceTypePhotoLibrary;
break;
}
}
else {
if (buttonIndex ==
0) {
return;
} else {
sourceType =
UIImagePickerControllerSourceTypeSavedPhotosAlbum;
}
}
//
跳转到相机或相册页面
UIImagePickerController *imagePickerController = [[UIImagePickerControlleralloc]
init];
imagePickerController.delegate =
self;
imagePickerController.allowsEditing =
YES;
imagePickerController.sourceType = sourceType;
[self
presentViewController:imagePickerControlleranimated:YEScompletion:^{}];
}
}
- (void)chooseImage:(UIButton *)btn {
UIActionSheet *sheet;
// 判断是否支持相机
if([UIImagePickerControllerisSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera])
{
sheet = [[UIActionSheetalloc]
initWithTitle:@"选择"delegate:selfcancelButtonTitle:nildestructiveButtonTitle:@"取消"otherButtonTitles:@"拍照",@"从相册选择",nil];
}
else {
sheet = [[UIActionSheetalloc]
initWithTitle:@"选择"delegate:selfcancelButtonTitle:nildestructiveButtonTitle:@"取消"otherButtonTitles:@"从相册选择",nil];
}
sheet.tag =
255;
[sheet showInView:self.view];
}
@end
2.奇怪的是我们不签代理,功能也能实现,为了保险起见,还是签订代理;到此,要实现的基本功能已经完全展示出来了,我们再根据自己项目的需求来改动就可以了。
3.希望对大家有所帮助。
1.我们在调用这个功能的时候,一般都有个用来填充图片的ImageView和点击ImageView触发此方法的事件,这里我就写个ImageView和Button来演示,下面是实现整个功能的代码:
#import "ViewController.h"
#define kWidth [UIScreen mainScreen].bounds.size.width
#define kHeight [UIScreen mainScreen].bounds.size.height
@interface
ViewController ()<UIActionSheetDelegate,UIImagePickerControllerDelegate>
{
BOOL isFullScreen;
}
@property(nonatomic,strong)UIImageView *imageView;
@end
@implementation ViewController
- (void)viewDidLoad {
[superviewDidLoad];
_imageView = [[UIImageViewalloc]initWithFrame:CGRectMake(100,100,
200,
200)];
_imageView.backgroundColor = [UIColorgrayColor];
[self.viewaddSubview:_imageView];
UIButton *chooseBtn = [UIButtonbuttonWithType:UIButtonTypeCustom];
chooseBtn.frame =
CGRectMake(100,
30, 100,
40);
[chooseBtn addTarget:selfaction:@selector(chooseImage:)forControlEvents:UIControlEventTouchUpInside];
[chooseBtn setBackgroundColor:[UIColorbrownColor]];
[chooseBtn setTitle:@"选择照片"forState:UIControlStateNormal];
[self.viewaddSubview:chooseBtn];
}
#pragma mark -
保存图片至沙盒
- (void) saveImage:(UIImage *)currentImage withName:(NSString *)imageName
{
NSData *imageData =
UIImageJPEGRepresentation(currentImage,
0.5);
//
获取沙盒目录
NSString *fullPath = [[NSHomeDirectory()stringByAppendingPathComponent:@"Documents"]stringByAppendingPathComponent:imageName];
//
将图片写入文件
[imageData writeToFile:fullPath
atomically:NO];
}
#pragma mark - image picker delegte
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
[picker dismissViewControllerAnimated:YEScompletion:^{}];
UIImage *image = [infoobjectForKey:UIImagePickerControllerOriginalImage];
[selfsaveImage:imagewithName:@"currentImage.png"];
NSString *fullPath = [[NSHomeDirectory()stringByAppendingPathComponent:@"Documents"]stringByAppendingPathComponent:@"currentImage.png"];
UIImage *savedImage = [[UIImagealloc]
initWithContentsOfFile:fullPath];
isFullScreen =NO;
[self.imageViewsetImage:savedImage];
self.imageView.tag =100;
}
- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker
{
[selfdismissViewControllerAnimated:YEScompletion:^{}];
}
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
isFullScreen = !isFullScreen;
UITouch *touch = [touches
anyObject];
CGPoint touchPoint = [touch
locationInView:self.view];
CGPoint imagePoint =
self.imageView.frame.origin;
//touchPoint.x
,touchPoint.y
就是触点的坐标
//
触点在imageView内,点击imageView时放大,再次点击时缩小
if(imagePoint.x <= touchPoint.x && imagePoint.x +self.imageView.frame.size.width
>=touchPoint.x && imagePoint.y <= touchPoint.y && imagePoint.y+self.imageView.frame.size.height
>= touchPoint.y)
{
//
设置图片放大动画
[UIViewbeginAnimations:nilcontext:nil];
// 动画时间
[UIViewsetAnimationDuration:1];
if (isFullScreen) {
// 放大尺寸
self.imageView.frame =CGRectMake(0,0,
kWidth, kHeight);
}
else {
// 缩小尺寸
self.imageView.frame =CGRectMake(100,100,
200, 200);
}
// commit动画
[UIViewcommitAnimations];
}
}
#pragma mark - actionsheet delegate
-(void) actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex
{
if (actionSheet.tag ==255) {
NSUInteger sourceType =
0;
//
判断是否支持相机
if([UIImagePickerControllerisSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera])
{
switch (buttonIndex) {
case
0:
//
取消
return;
case
1:
//
相机
sourceType =
UIImagePickerControllerSourceTypeCamera;
break;
case
2:
//
相册
sourceType =
UIImagePickerControllerSourceTypePhotoLibrary;
break;
}
}
else {
if (buttonIndex ==
0) {
return;
} else {
sourceType =
UIImagePickerControllerSourceTypeSavedPhotosAlbum;
}
}
//
跳转到相机或相册页面
UIImagePickerController *imagePickerController = [[UIImagePickerControlleralloc]
init];
imagePickerController.delegate =
self;
imagePickerController.allowsEditing =
YES;
imagePickerController.sourceType = sourceType;
[self
presentViewController:imagePickerControlleranimated:YEScompletion:^{}];
}
}
- (void)chooseImage:(UIButton *)btn {
UIActionSheet *sheet;
// 判断是否支持相机
if([UIImagePickerControllerisSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera])
{
sheet = [[UIActionSheetalloc]
initWithTitle:@"选择"delegate:selfcancelButtonTitle:nildestructiveButtonTitle:@"取消"otherButtonTitles:@"拍照",@"从相册选择",nil];
}
else {
sheet = [[UIActionSheetalloc]
initWithTitle:@"选择"delegate:selfcancelButtonTitle:nildestructiveButtonTitle:@"取消"otherButtonTitles:@"从相册选择",nil];
}
sheet.tag =
255;
[sheet showInView:self.view];
}
@end
2.奇怪的是我们不签代理,功能也能实现,为了保险起见,还是签订代理;到此,要实现的基本功能已经完全展示出来了,我们再根据自己项目的需求来改动就可以了。
3.希望对大家有所帮助。
相关文章推荐
- iOS开发之路--微博“更多”页面
- iOS开发之路--微博骨架搭建
- iOS开发使用JSON解析网络数据
- IOS开发代码分享之获取启动画面图片的string
- iOS开发实现音频播放功能
- iOS开发之视图切换
- iOS开发使用XML解析网络数据
- IOS开发实现录音功能
- iOS开发之UIScrollView详解
- IOS开发之路--C语言数组和字符串
- iOS开发之触摸事件
- IOS开发代码分享之用nstimer实现倒计时功能
- iOS开发使用GDataXML框架解析网络数据
- iOS开发之UITableView详解
- IOS开发代码分享之设置UISearchBar的背景颜色
- ios推送证书
- iOS和tvOS游戏按需加载资源简介
- ios开发之环境搭建-01
- 让iOS开发变得更有效率-分类、工具类