@interface ViewController ()
10年积累的网站设计制作、做网站经验,可以快速应对客户对网站的新想法和需求。提供各种问题对应的解决方案。让选择我们的客户得到更好、更有力的网络服务。我虽然不认识你,你也不认识我。但先网站制作后付款的网站建设流程,更有兴安免费网站建设让你可以放心的选择与我们合作。
@end
@implementation ViewController
- (void)viewDidLoad {
[superviewDidLoad];
UIButton *butt = [UIButtonbuttonWithType:UIButtonTypeCustom];
butt.frame = CGRectMake(100, 100, 50, 50);
[buttsetTitle:@"点击"forState:UIControlStateNormal];
[buttsetBackgroundColor:[UIColorgrayColor]];
[buttaddTarget:selfaction:@selector(goShowAlertView:)forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:butt];
}
- (void)goShowAlertView:(id)sender
{
double version = [[UIDevicecurrentDevice].systemVersiondoubleValue];//判定系统版本。
if(version>=8.0f){
UIAlertController *alertController = [UIAlertControlleralertControllerWithTitle:@"提示"message:nilpreferredStyle:UIAlertControllerStyleActionSheet];
[alertControlleraddAction:[UIAlertActionactionWithTitle:@"拍照"style:UIAlertActionStyleDefaulthandler:^(UIAlertAction * _Nonnull action) {
[selfselecetPhotoAndLibary:0];
}]];
[alertControlleraddAction:[UIAlertActionactionWithTitle:@"相册"style:UIAlertActionStyleDefaulthandler:^(UIAlertAction * _Nonnull action) {
[selfselecetPhotoAndLibary:1];
}]];
[alertControlleraddAction:[UIAlertActionactionWithTitle:@"取消"style:UIAlertActionStyleDestructivehandler:^(UIAlertAction * _Nonnull action) {
NSLog(@"取消");
}]];
[selfpresentViewController:alertControlleranimated:YEScompletion:nil];
}else{
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wdeprecated-declarations"
UIActionSheet *sheetView = [[UIActionSheetalloc]initWithTitle:@"提示"delegate:selfcancelButtonTitle:nildestructiveButtonTitle:nilotherButtonTitles:@"拍照",@"相册",@"取消", nil];
#pragma clang diagnostic pop
sheetView.actionSheetStyle = UIActionSheetStyleDefault;
[sheetView showInView:self.view];
}
}
#pragma mark actionSheet_delegate 点击事件
- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex NS_DEPRECATED_IOS(2_0, 8_3) __TVOS_PROHIBITED{
[selfselecetPhotoAndLibary:buttonIndex];
}
#pragma mark-one anTag 1是拍照,2是相册
- (void)selecetPhotoAndLibary:(NSInteger)anTag{
switch (anTag) {
case 0:
{
UIImagePickerControllerSourceType sourceType =UIImagePickerControllerSourceTypeCamera;
if ([UIImagePickerControllerisSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera])
{
UIImagePickerController *picker = [[UIImagePickerControlleralloc]init];
picker.delegate = self;
//设置拍照后的图片可被编辑
picker.sourceType=UIImagePickerControllerSourceTypeCamera;
picker.allowsEditing = YES;
picker.sourceType = sourceType;
[selfpresentViewController:pickeranimated:YEScompletion:nil];
}
else
{
UIAlertView *aletView = [[UIAlertViewalloc]initWithTitle:@"提示"message:@"模拟器不可用"delegate:nil
cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
[aletView show];
}
break;
}
case 1:
{
UIImagePickerController *picker=[[UIImagePickerControlleralloc]init];
picker.delegate=self;
picker.allowsEditing=YES;
[selfpresentViewController:pickeranimated:YEScompletion: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 = [NSDatedateWithTimeIntervalSinceNow:0];
NSTimeInterval last = [dattimeIntervalSince1970]*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_picpathViewsatomically:YES];
[selfdismissViewControllerAnimated:YEScompletion:^{
}];
}
#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
{
[selfdismissViewControllerAnimated:YES completion:^{
}];
}
文章题目:iOS 拍照和相机的基本使用
网页URL:http://lswzjz.com/article/jgpspi.html