温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

iOS 拍照和相机的基本使用

发布时间:2020-07-24 08:05:27 来源:网络 阅读:289 作者:大头狼小鬼 栏目:移动开发

@interface ViewController ()<UINavigationControllerDelegate, UIImagePickerControllerDelegate,UIActionSheetDelegate>


@end


@implementation ViewController


- (void)viewDidLoad {

    [super viewDidLoad];

    UIButton *butt = [UIButton buttonWithType:UIButtonTypeCustom];

    butt.frame = CGRectMake(100, 100, 50, 50);

    [butt setTitle:@"点击" forState:UIControlStateNormal];

    [butt setBackgroundColor:[UIColor grayColor]];

    [butt addTarget:self action:@selector(goShowAlertView:) forControlEvents:UIControlEventTouchUpInside];

    [self.view addSubview:butt];

    


}


- (void)goShowAlertView:(id)sender

{

 double version = [[UIDevice currentDevice].systemVersion doubleValue];//判定系统版本。

 if(version>=8.0f){

     UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"提示" message:nil preferredStyle:UIAlertControllerStyleActionSheet];

     [alertController addAction:[UIAlertAction actionWithTitle:@"拍照" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {

         [self selecetPhotoAndLibary:0];

     }]];

     [alertController addAction:[UIAlertAction actionWithTitle:@"相册" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {

         [self selecetPhotoAndLibary:1];

     }]];

     [alertController addAction:[UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleDestructive handler:^(UIAlertAction * _Nonnull action) {

         NSLog(@"取消");

     }]];

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

 

 }else{

     #pragma clang diagnostic push

     #pragma clang diagnostic ignored "-Wdeprecated-declarations"

     UIActionSheet *sheetView = [[UIActionSheet alloc] initWithTitle:@"提示" delegate:self cancelButtonTitle:nil destructiveButtonTitle:nil otherButtonTitles:@"拍照",@"相册",@"取消", nil];

     #pragma clang diagnostic pop

     sheetView.actionSheetStyleUIActionSheetStyleDefault;

     [sheetView showInView:self.view];

     }


}


#pragma mark actionSheet_delegate 点击事件

- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex NS_DEPRECATED_IOS(2_0, 8_3) __TVOS_PROHIBITED{

    [self selecetPhotoAndLibary:buttonIndex];

}



#pragma mark-one  anTag 1是拍照,2是相册

- (void)selecetPhotoAndLibary:(NSInteger)anTag{

    

    switch (anTag) {

        case 0:

        {

            UIImagePickerControllerSourceType sourceType = UIImagePickerControllerSourceTypeCamera;

            if ([UIImagePickerController isSourceTypeAvailable: UIImagePickerControllerSourceTypeCamera])

            {

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

                picker.delegate = self;

                //设置拍照后的图片可被编辑

                picker.sourceType=UIImagePickerControllerSourceTypeCamera;

                picker.allowsEditing = YES;

                picker.sourceType = sourceType;

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

            }

            else

            {

                UIAlertView *aletView = [[UIAlertView alloc] initWithTitle:@"提示" message:@"模拟器不可用" delegate:nil

                                                         cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];

                [aletView show];

            }

            

            break;


        }

        case 1:

        {

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

            picker.delegate=self;

            picker.allowsEditing=YES;

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

            break;

        }

        default:

            break;

    }

    

   

}


#pragma mark-onnce 代理方法

- (void)p_w_picpathPickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info{


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

    editedImage = [self makeThumbnailFromImage:editedImage scale:1.0f];

    

    NSString *homePath = [NSHomeDirectory() stringByAppendingString:@"/Documents"];

    NSDate* dat = [NSDate dateWithTimeIntervalSinceNow:0];

    NSTimeInterval last = [dat timeIntervalSince1970]*1000;

    NSString *p_w_picpathViews   = [homePath stringByAppendingFormat:@"/%f.jpeg", last];

   //路径,图片的本地路径 NSString *p_w_picpathPath = [NSString stringWithFormat:@"/%f.jpeg", last];

    [UIImageJPEGRepresentation(editedImage, 1.0f) writeToFile:p_w_picpathViews atomically:YES];

    [self dismissViewControllerAnimated:YES completion:^{

        

    }];

    

}


#pragma mark-onnce 手动实现图片压缩,可以写到分类里,封装成常用方法。按照大小进行比例压缩,改变了图片的size

- (UIImage *)makeThumbnailFromImage:(UIImage *)srcImage scale:(double)p_w_picpathScale {

    UIImage *thumbnail = nil;

    CGSize p_w_picpathSize = CGSizeMake(srcImage.size.width * p_w_picpathScale, srcImage.size.height * p_w_picpathScale);

    if (srcImage.size.width != p_w_picpathSize.width || srcImage.size.height != p_w_picpathSize.height)

    {

        UIGraphicsBeginImageContext(p_w_picpathSize);

        CGRect p_w_picpathRect = CGRectMake(0.0, 0.0, p_w_picpathSize.width, p_w_picpathSize.height);

        [srcImage drawInRect:p_w_picpathRect];

        thumbnail = UIGraphicsGetImageFromCurrentImageContext();

        UIGraphicsEndImageContext();

    }

    else

    {

        thumbnail = srcImage;

    }

    return thumbnail;

}

#pragma mark-onnce 取消的代理方法

- (void)p_w_picpathPickerControllerDidCancel:(UIImagePickerController *)picker

{

    [self dismissViewControllerAnimated:YES completion:^{

    }];

}



向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI