溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

iOS 拍照和相機的基本使用

發(fā)布時間:2020-07-24 08:05:27 來源:網絡 閱讀:288 作者:大頭狼小鬼 欄目:移動開發(fā)

@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];//判定系統(tǒng)版本。

 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問一下細節(jié)

免責聲明:本站發(fā)布的內容(圖片、視頻和文字)以原創(chuàng)、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。

AI