美文网首页
调用系统相册

调用系统相册

作者: CN_HarrySun | 来源:发表于2017-04-17 10:27 被阅读25次

遇到一个问题:相机的界面总是显示英文。
查了下资料发现在 info.plist里面添加Localized resources can be mixed = YES ,表示是否允许应用程序获取框架库内语言(前题是手机要设置为中文)

1.首先遵循协议

<UIImagePickerControllerDelegate,UINavigationControllerDelegate,UIActionSheetDelegate>

2.根据需要选择调用相机/相册

这里我们一般会使用UIActionSheet中(需要遵循UIActionSheetDelegate协议)

UIActionSheet *actionSheet = [[UIActionSheet alloc] initWithTitle:nil delegate:self cancelButtonTitle:@"取消" destructiveButtonTitle:nil otherButtonTitles:@"拍照",@"从相册选择", nil];
actionSheet.actionSheetStyle = UIActionSheetStyleDefault;
[actionSheet showInView:self.view];
UIActionSheetDelegate

根据被点击的按钮做出反应,0对应destructiveButton,之后的button依次排序

#pragma mark - UIActionSheetDelegate
- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex {
    
    if (buttonIndex == 0) {
        
        NSLog(@"拍照");
        if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) {
            
            UIImagePickerController *imageController = [[UIImagePickerController alloc] init];
            [imageController setSourceType:UIImagePickerControllerSourceTypeCamera];    // 设置类型
            [imageController setDelegate:self];
            [imageController setAllowsEditing:YES];    // 设置是否可以编辑
            [self presentViewController:imageController animated:YES completion:nil];
        }else{
            
            [self showAlertWith:@"提示" andMessage:@"哎呀,当前设备没有摄像头。"];
        }
    }else if (buttonIndex == 1) {
        
        NSLog(@"相册");
        if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypePhotoLibrary]) {
            UIImagePickerController *imageController = [[UIImagePickerController alloc] init];
            [imageController setSourceType:UIImagePickerControllerSourceTypePhotoLibrary];// 设置为相册类型
            [imageController setDelegate:self];
            [imageController setAllowsEditing:YES];
            [self presentViewController:imageController animated:YES completion:nil];
        }else{
            
            [self showAlertWith:@"提示" andMessage:@"图片库不可用。"];
        }
    }
}
UIImagePickerControllerDelegate

当获取到照片或视频后调用

#pragma mark UIImagePickerControllerDelegate
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {
    
    NSLog(@"获取成功");
    UIImage *pickedImage = nil;
    if ([picker allowsEditing]) {
        pickedImage = [info objectForKey:UIImagePickerControllerEditedImage];
    } else {
        pickedImage = [info objectForKey:UIImagePickerControllerOriginalImage];
    }
    NSString *imagePath = [[NSHomeDirectory() stringByAppendingPathComponent:@"Documents"] stringByAppendingPathComponent:@"image.png"];
    [UIImagePNGRepresentation(pickedImage) writeToFile:imagePath atomically:YES];

     self.imageView.image = [UIImage imageNamed:pickedImage];    // 使用
}

相关文章

  • 3.5 ios调用系统的相册加载头像

    ios调用系统的相册加载头像 ios调用系统的相册加载头像.png

  • iOS获取、写入系统相册图片

    为什么要调用系统相册 现在很多项目都会用到调用系统相册,例如保存图片到系统相册、选取相册中的图片、给联系人设置头像...

  • 调用系统相册

    遇到一个问题:相机的界面总是显示英文。查了下资料发现在 info.plist里面添加Localized resou...

  • IOS11系统相册问题

    调用系统UIImagePickerController相册时候出现UIImagePickerController ...

  • iOS调用系统相册

    iOS10.0出现了挺多坑的,访问系统的相机、麦克风、相册等都得进行设置。今天写项目时,需要调用相机以及相册,添加...

  • iOS调用系统相册

    话不多说,直接上代码 UIImagePickerControllerDelegate协议里面的方法,注意先去遵循协...

  • iOS 调用系统相册

    在inof.pilst中的设置 代码部分

  • iOS 使用系统相机、相册显示中文

    在调用系统相机时,"cancel" 改为"取消",调用系统相册时,"photos"改为 "相机",方法如下: 1....

  • 工作小结

    在调用系统相机时,"cancel" 改为"取消",调用系统相册时,"photos" 改为 "相机",方法如下: 1...

  • iOS

    iOS调用系统功能 [1] 相册、相机 [2] WiFi、BlueTooth

网友评论

      本文标题:调用系统相册

      本文链接:https://www.haomeiwen.com/subject/isttzttx.html