zoukankan      html  css  js  c++  java
  • 相机相册

    //相机

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

    }

  • 相关阅读:
    正则表达式记录
    XMLHttpRequest(Ajax)不能设置自定义的Referer
    删除右键菜单上的Adobe Drive CS4,Win7上也没有问题
    A HOWTO on Optimizing PHP(如何优化PHP的一篇文章)
    PHP XML To Array,将XML转换为数组
    让图片在高度确定的块元素中垂直居中
    (转)2011年,還是微軟IE的天下~網頁設計師哭泣吧!
    产生类似GUID的唯一ID
    转:编写跨浏览器兼容的 CSS 代码的金科玉律
    array_intersect 比 array_diff 快
  • 原文地址:https://www.cnblogs.com/liuyang666/p/5288358.html
Copyright © 2011-2022 走看看