iOS选择头像示例代码

时间:2021-12-25 20:52:15
 - (void)tap:(UITapGestureRecognizer *)tapGR{
    NSLog(@"被点按了");
    
    UIActionSheet *as = [[UIActionSheet alloc]initWithTitle:nil delegate:self cancelButtonTitle:@"取消" destructiveButtonTitle:@"从手机相册获取" otherButtonTitles:@"打开照相机", nil];
    [as showInView:self.view];
}



- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex{
    switch (buttonIndex) {
        case 0:
            [self gotoPhoto];
            break;
        case 1:
            [self gotoCamera];
            break;
    }

}

-(void)gotoPhoto{
    NSLog(@"照片");
    
    UIImagePickerController*pickerFromPhotoLibrary = [[UIImagePickerController a
lloc]init];
    pickerFromPhotoLibrary.delegate = self;
    pickerFromPhotoLibrary.allowsEditing = YES;
    pickerFromPhotoLibrary.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
    [self presentViewController:pickerFromPhotoLibrary animated:YES completion:nil];
    
}

-(void)gotoCamera{
    if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) {
        UIImagePickerController *ipc = [[UIImagePickerController alloc]init];
        [ipc setSourceType:UIImagePickerControllerSourceTypeCamera];
        ipc.delegate = self;
        ipc.allowsEditing = YES;
        [self presentViewController:ipc animated:YES completion:nil];
    }else{
       
 UIAlertView*alert = [[UIAlertView alloc]initWithTitle:@"提示" message:@"该设备暂不能使用" delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"确定", nil];
        [alert show];
        NSLog(@"这设备没相机");
    }
}


- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker{
    
    [self dismissViewControllerAnimated:YES completion:nil];
}

- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info{
    self.image = [info objectForKey:@"UIImagePickerControllerEditedImage"];
    self.iv.image=self.image;
    [self dismissViewControllerAnimated:YES completion:nil];
}