iOS 10 关于相机拍照和图片单张选取

本文介绍如何在iOS应用中集成相册选取和相机拍摄功能,包括使用UIImagePickerController选择图片,调整图片尺寸,并将拍摄的照片保存到相册。

#import "ViewController.h"

#import "GTMBase64.h"

#import <AssetsLibrary/AssetsLibrary.h>

@interface ViewController ()<UIImagePickerControllerDelegate,UINavigationControllerDelegate>

@property (nonatomic,strong) UIViewController * parentView;

@end


@implementation ViewController


- (void)viewDidLoad {

    [super viewDidLoad];

    // Do any additional setup after loading the view, typically from a nib.

}



- (void)didReceiveMemoryWarning {

    [super didReceiveMemoryWarning];

    // Dispose of any resources that can be recreated.

}


- (IBAction)photosOrcamrae:(id)sender {

    

    UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"选择" message:nil preferredStyle: UIAlertControllerStyleActionSheet];

    

    UIAlertAction *deleteAction = [UIAlertAction actionWithTitle:@"拍照" style:UIAlertActionStyleDestructive handler:^(UIAlertAction *deleteAction){

        [self takePictureClick];

    }];

    UIAlertAction *archiveAction = [UIAlertAction actionWithTitle:@"相册中选取" style:UIAlertActionStyleDefault handler:^(UIAlertAction *archiveAction){

        [self pictureBtnClick];

    }];

    

    UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction *cancelAction){

        

    }];

    

    [alertController addAction:cancelAction];

    [alertController addAction:deleteAction];

    [alertController addAction:archiveAction];

    [self presentViewController:alertController animated:YES completion:nil];

    

}

//调用相机

- (void)takePictureClick{

    

    if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera])

    {

        UIImagePickerController *pic = [[UIImagePickerController alloc] init];

        pic.sourceType = UIImagePickerControllerSourceTypeCamera;

        pic.allowsEditing = YES;

        pic.showsCameraControls = YES;

        pic.delegate =self;

        [self presentViewController:pic animated:YES completion:nil];

    }

}

- (void)image:(UIImage *)image didFinishSavingWithError:

(NSError *)error contextInfo:(void *)contextInfo;

{

    // Handle the end of the image write process

    if (!error){

        NSLog(@"Image written to photo album");

    }else{

        NSLog(@"Error writing to photo album: %@",

                  [error localizedDescription]);

    }

}

//相册选取图片

- (void)pictureBtnClick{

    

    UIImagePickerController *ipc = [[UIImagePickerController alloc] init];

    ipc.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;

    ipc.modalTransitionStyle = UIModalTransitionStyleCoverVertical;

    ipc.allowsEditing = YES;

    ipc.delegate =self;

    [self presentViewController:ipc animated:YES completion:nil];

    

}


#pragma mark - ImagePickerDelegate


-(UIImage*)imageWithImage:(UIImage*)image scaledToSize:(CGSize)newSize

{

    UIGraphicsBeginImageContext(newSize);

    [image drawInRect:CGRectMake(0,0,newSize.width,newSize.height)];

    UIImage* newImage = UIGraphicsGetImageFromCurrentImageContext();

    return newImage;

}


- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info

{

    //相册和相机拍摄的照片选择都走这个代理

   

    UIImage *image = [info objectForKey:@"UIImagePickerControllerEditedImage"];

    

    CGSize imagesize = image.size;

    imagesize.height =200;

    imagesize.width =200;

    UIImage *img = [self imageWithImage:image scaledToSize:imagesize];

    

    [self updateUserHeaderWithImage:img];

    

    [self dismissViewControllerAnimated:YES completion:nil];

}


- (void)updateUserHeaderWithImage:(UIImage *)image {

    NSData *imageData = UIImageJPEGRepresentation(image, 1.0);

    NSString *imageStr = [GTMBase64 stringByEncodingData:imageData];

    //相机拍摄照片存储照片

    if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera])

    {

        ALAssetsLibrary *library = [[ALAssetsLibrary alloc] init];

        [library writeImageToSavedPhotosAlbum:[image CGImage] orientation:(ALAssetOrientation)[image imageOrientation] completionBlock:^(NSURL *assetURL, NSError *error){

            if (error) {

                //  error handling

            } else {

                //  success handling

            }

        }];

    }

   //这里是选择的照片 进行处理

}


@end

个人手写,不喜勿喷,下次发布仿写相册多张选取和相机拍照以后的多张选取

干货才是硬道理


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值