iOS调用系统的相册(包括,显示中文的标题)

  • Post author:
  • Post category:其他


首先在info.plist加上这一条这样就可以使我们调出来的相册显示出中文啦~

Localized resources can be mixed 设置为 YES。

下面我来简单介绍下,怎么调用系统相机和系统相册来更换图片~

首先,我们要遵守几个代理啦~

UIImagePickerControllerDelegate, UINavigationControllerDelegate, UIActionSheetDelegate
前两个是和调用相机和相册有关,后面这个是和下面弹出的框框有关~
点击头像触发下面这个方法
<pre name="code" class="objc">-(void)headImgChange:(id)sender
{
    UIActionSheet *actionSheet = [[UIActionSheet alloc] initWithTitle:nil delegate:self cancelButtonTitle:@"取消" destructiveButtonTitle:nil otherButtonTitles:@"从相册中选择", @"打开拍照", nil];
    [actionSheet showInView:self.view];
}
然后我们选择的时候会触发下面的方法~
#pragma mark - UIActionSheet Delegate
-(void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex
{
    switch (buttonIndex) {
        case 0:
        {
            //从相册中选择
            [self LocalPhoto];
        }
            break;
        case 1:
        {
            //拍照
            [self takePhoto];
        }
            break;
    }
}
<pre name="code" class="objc">//打开本地相册
-(void)LocalPhoto
{
    UIImagePickerController *picker = [[UIImagePickerController alloc] init];
    
    picker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
    picker.delegate = self;
    //设置选择后的图片可被编辑
    picker.allowsEditing = YES;
    [self presentViewController:picker animated:YES completion:nil];
}
<pre name="code" class="objc">//开始拍照(这个要用真机测试,模拟器测试会crash~)
-(void)takePhoto
{
    UIImagePickerControllerSourceType sourceType = UIImagePickerControllerSourceTypeCamera;
    if ([UIImagePickerController isSourceTypeAvailable: UIImagePickerControllerSourceTypeCamera])
    {
        UIImagePickerController *picker = [[UIImagePickerController alloc] init];
        picker.delegate = self;
        //设置拍照后的图片可被编辑
        picker.allowsEditing = YES;
        picker.sourceType = sourceType;
        [self presentViewController:picker animated:YES completion:nil];
    }else
    {
    }
}




最后我们从相册选择也好,拍照也好,完成后都会到这个代理方法里

//当选择一张图片后进入这里
-(void)imagePickerController:(UIImagePickerController*)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
    NSString *type = [info objectForKey:UIImagePickerControllerMediaType];
    //当选择的类型是图片
    if ([type isEqualToString:@"public.image"])
    {
        //先把图片转成NSData
        UIImage* image = [info objectForKey:@"UIImagePickerControllerOriginalImage"];
        if (UIImagePNGRepresentation(image) == nil)
        {
            m_imgData = UIImageJPEGRepresentation(image, 1.0);
            m_dataType = @"image/jpeg";
        }else{
            m_imgData = UIImagePNGRepresentation(image);
            m_dataType = @"image/png";
        }
        //关闭相册界面
        [picker dismissModalViewControllerAnimated:YES];
        [self.headButton setBackgroundImage:image forState:UIControlStateNormal];
        //开启上传
//        [self uploadUserFace];
    }
}

基本完事了,欢迎大家多多交流.喜欢交流的朋友请加qq群:410033829,方便交流和跳槽~



版权声明:本文为dlmyliyi原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。