iOS调用系统相机,访问系统相册功能实现

        我们在做应用过程中,难免会遇到要设置用户头像这样的功能,我这里总结了一个调用系统相机,相册的功能实现,写出来与大家分享,如有不足还请指正:

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.希望对大家有所帮助。



  • 3
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值