博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
相机相册
阅读量:6969 次
发布时间:2019-06-27

本文共 4084 字,大约阅读时间需要 13 分钟。

//相机

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

}

 

转载于:https://www.cnblogs.com/liuyang666/p/5288358.html

你可能感兴趣的文章
自建docker swarm体验简单之美
查看>>
微信定制开发怎么做?
查看>>
LeetCode Unique Paths
查看>>
Memcachedclientutils类
查看>>
POJ - 3352 Road Construction(边双连通分量)
查看>>
【转】Castle Windsor之组件注册
查看>>
免费馅饼
查看>>
Python模块包(pycharm右键创建文件夹和python package的区别)中__init__.py文件的作用
查看>>
SQL Server中有关约束(constraint)的一些细节
查看>>
使用Nginx实现灰度发布
查看>>
(转载)WPF中的动画——(一)基本概念
查看>>
HDU4876:ZCC loves cards
查看>>
用C语言实现websocket服务器
查看>>
python读取.mat文件
查看>>
JAVA-JSP注释
查看>>
RSA加密异常
查看>>
搭建自己的iOS内测分发平台
查看>>
Unix下C语言开发工具
查看>>
AXURE插件在 Chrome 浏览器中用不了怎么办?
查看>>
css 文本超出2行就隐藏并且显示省略号
查看>>