相机相册

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了相机相册相关的知识,希望对你有一定的参考价值。

//相机

-(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

    {

        NSLog(@"模拟其中无法打开照相机,请在真机中使用");

    }

}

 

//打开本地相册

-(void)LocalPhoto

{

    UIImagePickerController *picker = [[UIImagePickerController alloc] init];

    

    picker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;

    picker.delegate = self;

    //设置选择后的图片可被编辑

    picker.allowsEditing = YES;

    [self presentViewController:picker animated:YES completion:nil];

}

 

-(void)imagePickerController:(UIImagePickerController*)picker didFinishPickingMediaWithInfo:(NSDictionary *)info

 

{

    

    NSString *type = [info objectForKey:UIImagePickerControllerMediaType];

    

    //当选择的类型是图片

    if ([type isEqualToString:@"public.image"])

    {

        //先把图片转成NSData

        UIImage* image = [info objectForKey:@"UIImagePickerControllerOriginalImage"];

        NSData *data;

        if (UIImagePNGRepresentation(image) == nil)

        {

            data = UIImageJPEGRepresentation(image, 0);

        }

        else

        {

            data = UIImagePNGRepresentation(image);

        }

        

        //图片保存的路径

        //这里将图片放在沙盒的documents文件夹中

        NSString * DocumentsPath = [NSHomeDirectory() stringByAppendingPathComponent:@"Documents"];

        

        //文件管理器

        NSFileManager *fileManager = [NSFileManager defaultManager];

        

        //把刚刚图片转换的data对象拷贝至沙盒中 并保存为image.png

        [fileManager createDirectoryAtPath:DocumentsPath withIntermediateDirectories:YES attributes:nil error:nil];

        [fileManager createFileAtPath:[DocumentsPath stringByAppendingString:@"/image.png"] contents:data attributes:nil];

        

        //得到选择后沙盒中图片的完整路径

 

        //关闭相册界面

        [picker dismissViewControllerAnimated:YES completion:nil];

        

        UIGraphicsBeginImageContext(self.headImageButton.frame.size);

        [image drawInRect:CGRectMake(0, 0, self.headImageButton.frame.size.width, self.headImageButton.frame.size.height)];

        

        UIImage *newImage=UIGraphicsGetImageFromCurrentImageContext();

        

        

        

        

        //创建一个选择后图片的小图标放在下方

        //类似微薄选择图后的效果

        self.smallimage = [[UIImageView alloc] initWithFrame:CGRectMake(50, 120, 40, 40)];

        

       self.smallimage.image = newImage;

        

        

            dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{

        

                NSData *imageData = nil;

                NSString *mimeType = nil;

                

 

                imageData=UIImageJPEGRepresentation(self.smallimage.image, 1);

                _picture=[[NSString alloc]init];

                mimeType=[imageData base64EncodedStringWithOptions:NSDataBase64Encoding64CharacterLineLength];

                _picture= (__bridge NSString *) CFURLCreateStringByAddingPercentEscapes(kCFAllocatorDefault,(CFStringRef)mimeType,NULL,CFSTR(":/?#[]@!$&’()*+,;="),kCFStringEncodingUTF8);

                

                

                NSUserDefaults *userDef=[NSUserDefaults standardUserDefaults];

 

//                NSDictionary *bdeditInfo=[NSDictionary dictionaryWithObjectsAndKeys:_picture,@"gd_picture",nil];

                

                [userDef setObject:_picture forKey:@"gd_picture"];

 

 

                dispatch_async(dispatch_get_main_queue(), ^{

                });

        

            });

    

        //加在视图中

//        [self.headImageButton setImage:smallimage.image forState:UIControlStateNormal];

        [self.headImageButton setBackgroundImage:self.smallimage.image forState:UIControlStateNormal];

        self.headImageButton.layer.masksToBounds=YES;

        

        self.headImageButton.layer.cornerRadius=self.headImageButton.frame.size.width/2;

  

        NSData *headImageData;

        headImageData=[NSKeyedArchiver archivedDataWithRootObject:self.headImageButton.imageView.image];

        [[NSUserDefaults standardUserDefaults]setObject:headImageData forKey:@"headImage"];

        

        

    }

    

}

 

 

 

#pragma mark

- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {

    UITextField *myTextView1=[[UITextField alloc]init];

    myTextView1=(UITextField *)[self.view viewWithTag:101];

    if (![myTextView1 isExclusiveTouch]) {

        [myTextView1 resignFirstResponder];

    }

 

    

}

 

- (BOOL) imageHasAlpha: (UIImage *) image

{

    CGImageAlphaInfo alpha = CGImageGetAlphaInfo(image.CGImage);

    return (alpha == kCGImageAlphaFirst ||

            alpha == kCGImageAlphaLast ||

            alpha == kCGImageAlphaPremultipliedFirst ||

            alpha == kCGImageAlphaPremultipliedLast);

}

 

以上是关于相机相册的主要内容,如果未能解决你的问题,请参考以下文章

IOS-相机相册

iOS10以后相机相册等授权问题

iOS 4.2 相机和相册问题

iOS开发-iOS调用相机调用相册将图片保存到本地相册

iOS:确定相册中的照片是不是来自相机

调用系统相册或相机工具类