zoukankan      html  css  js  c++  java
  • iOS UIPickerController 调用相机,选择图片上传,带预览功能

    在.h中加入delegate

    @interface ViewController : UIViewController<UIActionSheetDelegate,UIImagePickerControllerDelegate,UINavigationControllerDelegate>

    //实现按钮事件

    -(IBAction)chooseImage:(id)sender {
         
        UIActionSheet *sheet;
    <p>
    // 判断是否支持相机
    </p>
    if([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera])
     
           {
               sheet  = [[UIActionSheet alloc] initWithTitle:@"选择" delegate:self cancelButtonTitle:nil destructiveButtonTitle:@"取消" otherButtonTitles:@"拍照",@"从相册选择", nil];
     
           }
     
        else {
             
            sheet = [[UIActionSheet alloc] initWithTitle:@"选择" delegate:self cancelButtonTitle:nil destructiveButtonTitle:@"取消" otherButtonTitles:@"从相册选择", nil];
     
        }
         
        sheet.tag = 255;
         
        [sheet showInView:self.view];
         
    }
    

    实现actionSheet delegate 事件

    判断是否支持相机,跳转到相机或相册界面

    -(void) actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex
    {
        if (actionSheet.tag == 255) {
             
            NSUInteger sourceType = 0;
             
            // 判断是否支持相机
            if([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) {
                 
                switch (buttonIndex) {
                    case 0:
                        // 取消
                        return;
                    case 1:
                        // 相机
                        sourceType = UIImagePickerControllerSourceTypeCamera;
                        break;
                         
                    case 2:
                        // 相册
                        sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
                        break;
                }
            }
            else {
                if (buttonIndex == 0) {
                     
                    return;
                } else {
                    sourceType = UIImagePickerControllerSourceTypeSavedPhotosAlbum;
                }
            }
    <p>
    // 跳转到相机或相册页面
    </p>
    UIImagePickerController *imagePickerController = [[UIImagePickerController alloc] init];
             
            imagePickerController.delegate = self;
             
            imagePickerController.allowsEditing = YES;
             
            imagePickerController.sourceType = sourceType;
             
            [self presentViewController:imagePickerController animated:YES completion:^{}];
             
            [imagePickerController release];
        }
    }
    

    实现ImagePicker] delegate 事件

    - (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
    {
        [picker dismissViewControllerAnimated:YES completion:^{}];
         
        UIImage *image = [info objectForKey:UIImagePickerControllerOriginalImage];
        /* 此处info 有六个值 
         * UIImagePickerControllerMediaType; // an NSString UTTypeImage)
         * UIImagePickerControllerOriginalImage;  // a UIImage 原始图片
         * UIImagePickerControllerEditedImage;    // a UIImage 裁剪后图片
         * UIImagePickerControllerCropRect;       // an NSValue (CGRect)
         * UIImagePickerControllerMediaURL;       // an NSURL   
         * UIImagePickerControllerReferenceURL    // an NSURL that references an asset in the AssetsLibrary framework
         * UIImagePickerControllerMediaMetadata    // an NSDictionary containing metadata from a captured photo
         */
        // 保存图片至本地,方法见下文
        [self saveImage:image withName:@"currentImage.png"];
         
        NSString *fullPath = [[NSHomeDirectory() stringByAppendingPathComponent:@"Documents"] stringByAppendingPathComponent:@"currentImage.png"];
         
        UIImage *savedImage = [[UIImage alloc] initWithContentsOfFile:fullPath];
         
        isFullScreen = NO;
        [self.imageView setImage:savedImage];
         
        self.imageView.tag = 100;
         
    }
    - (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker
    {
        [self dismissViewControllerAnimated:YES completion:^{}];
    }
    

     保存图片方法

    	
    #pragma mark - 保存图片至沙盒
    - (void) saveImage:(UIImage *)currentImage withName:(NSString *)imageName
    {
         
        NSData *imageData = UIImageJPEGRepresentation(currentImage, 0.5);
        // 获取沙盒目录
         
        NSString *fullPath = [[NSHomeDirectory() stringByAppendingPathComponent:@"Documents"] stringByAppendingPathComponent:imageName];
        // 将图片写入文件
         
        [imageData writeToFile:fullPath atomically:NO];
    }
    

    实现点击图片预览功能,滑动放大缩小,带动画

    -(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
    {
         
        isFullScreen = !isFullScreen;
        UITouch *touch = [touches anyObject];
         
        CGPoint touchPoint = [touch locationInView:self.view];
         
        CGPoint imagePoint = self.imageView.frame.origin;
        //touchPoint.x ,touchPoint.y 就是触点的坐标
         
        // 触点在imageView内,点击imageView时 放大,再次点击时缩小
        if(imagePoint.x <= touchPoint.x && imagePoint.x +self.imageView.frame.size.width >=touchPoint.x && imagePoint.y <=  touchPoint.y && imagePoint.y+self.imageView.frame.size.height >= touchPoint.y)
        {
            // 设置图片放大动画
            [UIView beginAnimations:nil context:nil];
            // 动画时间
            [UIView setAnimationDuration:1];
             
            if (isFullScreen) {
                // 放大尺寸
                 
                self.imageView.frame = CGRectMake(0, 0, 320, 480);
            }
            else {
                // 缩小尺寸
                self.imageView.frame = CGRectMake(50, 65, 90, 115);
            }
             
            // commit动画
            [UIView commitAnimations];
             
        }
         
    }
    
  • 相关阅读:
    SpringJDBC源码分析记录
    RHEL7使用NAT方式上网
    SQL优化参考
    IDEA引入Gradle工程小记
    OAuth2.0原理与实现
    sudo用法记录
    ZooKeeper单机伪集群搭建与启动
    Netty实践与NIO原理
    Spring Security原理与应用
    Winform 生成不需要安装的exe可执行文件 ILMerge使用
  • 原文地址:https://www.cnblogs.com/wangbinbin/p/4771855.html
Copyright © 2011-2022 走看看