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

来源:互联网 发布:java开发restful接口 编辑:程序博客网 时间:2024/05/16 01:42

#import "ViewController.h"

#import "GTMBase64.h"

#import <AssetsLibrary/AssetsLibrary.h>

@interface ViewController ()<UIImagePickerControllerDelegate,UINavigationControllerDelegate>

@property (nonatomic,strong)UIViewController * parentView;

@end


@implementation ViewController


- (void)viewDidLoad {

    [superviewDidLoad];

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

}



- (void)didReceiveMemoryWarning {

    [superdidReceiveMemoryWarning];

    // Dispose of any resources that can be recreated.

}


- (IBAction)photosOrcamrae:(id)sender {

    

    UIAlertController *alertController = [UIAlertControlleralertControllerWithTitle:@"选择"message:nilpreferredStyle: UIAlertControllerStyleActionSheet];

    

    UIAlertAction *deleteAction = [UIAlertActionactionWithTitle:@"拍照"style:UIAlertActionStyleDestructivehandler:^(UIAlertAction *deleteAction){

        [selftakePictureClick];

    }];

    UIAlertAction *archiveAction = [UIAlertActionactionWithTitle:@"相册中选取"style:UIAlertActionStyleDefaulthandler:^(UIAlertAction *archiveAction){

        [selfpictureBtnClick];

    }];

    

    UIAlertAction *cancelAction = [UIAlertActionactionWithTitle:@"取消"style:UIAlertActionStyleCancelhandler:^(UIAlertAction *cancelAction){

        

    }];

    

    [alertController addAction:cancelAction];

    [alertController addAction:deleteAction];

    [alertController addAction:archiveAction];

    [selfpresentViewController:alertControlleranimated:YEScompletion:nil];

    

}

//调用相机

- (void)takePictureClick{

    

    if ([UIImagePickerControllerisSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera])

    {

        UIImagePickerController *pic = [[UIImagePickerControlleralloc] init];

        pic.sourceType =UIImagePickerControllerSourceTypeCamera;

        pic.allowsEditing =YES;

        pic.showsCameraControls =YES;

        pic.delegate =self;

        [selfpresentViewController:picanimated:YEScompletion: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 = [[UIImagePickerControlleralloc] init];

    ipc.sourceType =UIImagePickerControllerSourceTypePhotoLibrary;

    ipc.modalTransitionStyle =UIModalTransitionStyleCoverVertical;

    ipc.allowsEditing =YES;

    ipc.delegate =self;

    [selfpresentViewController:ipcanimated:YEScompletion: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 = [infoobjectForKey:@"UIImagePickerControllerEditedImage"];

    

    CGSize imagesize = image.size;

    imagesize.height =200;

    imagesize.width =200;

    UIImage *img = [selfimageWithImage:image scaledToSize:imagesize];

    

    [selfupdateUserHeaderWithImage:img];

    

    [selfdismissViewControllerAnimated:YEScompletion:nil];

}


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

    NSData *imageData =UIImageJPEGRepresentation(image,1.0);

    NSString *imageStr = [GTMBase64stringByEncodingData:imageData];

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

    if ([UIImagePickerControllerisSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera])

    {

        ALAssetsLibrary *library = [[ALAssetsLibraryalloc] init];

        [library writeImageToSavedPhotosAlbum:[imageCGImage] orientation:(ALAssetOrientation)[imageimageOrientation] completionBlock:^(NSURL *assetURL,NSError *error){

            if (error) {

                //  error handling

            } else {

                //  success handling

            }

        }];

    }

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

}


@end

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

干货才是硬道理


0 0