• UIImagePickerController照片选取器


    记录于2013/7/4
     
    加入框架: 
    MobileCoreServices.framework 
    MediaPlayer.framework
     
    导入头文件:
    #import <MediaPlayer/MediaPlayer.h>
    #import <MobileCoreServices/UTCoreTypes.h> 
     

    代码: 
    if(![UIImagePickerControllerisSourceTypeAvailable:
              UIImagePickerControllerSourceTypeCamera]) {
            takePictureButton.hidden = YES; //无摄像头隐藏改按钮
        }
        imageFrame = imageView.frame;

    实现:

    - (IBAction)shootPictureOrVideo:(id)sender {
        [selfgetMediaFromSource:UIImagePickerControllerSourceTypeCamera];
    }
    - (IBAction)selectExistingPictureOrVideo:(id)sender {
        [selfgetMediaFromSource:UIImagePickerControllerSourceTypePhotoLibrary];
    }
    #pragma mark UIImagePickerController delegate methods
    - (void)imagePickerController:(UIImagePickerController *)picker
    didFinishPickingMediaWithInfo:(NSDictionary *)info {
        self.lastChosenMediaType = [info objectForKey:UIImagePickerControllerMediaType];
        if ([lastChosenMediaTypeisEqual:(NSString *)kUTTypeImage]) {
            UIImage *chosenImage = [info objectForKey:UIImagePickerControllerEditedImage];
            UIImage *shrunkenImage = shrinkImage(chosenImage, imageFrame.size);
            self.image = shrunkenImage;
        } elseif ([lastChosenMediaTypeisEqual:(NSString *)kUTTypeMovie]) {
            self.movieURL = [info objectForKey:UIImagePickerControllerMediaURL];
        }
        [picker dismissModalViewControllerAnimated:YES];
    }
    - (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker {   
        [picker dismissModalViewControllerAnimated:YES];
    }
    #pragma mark  -
    //类扩张
    staticUIImage *shrinkImage(UIImage *original, CGSize size) {
        //设备物理屏幕像素倍数
        CGFloat scale = [UIScreenmainScreen].scale;
        CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
       
        CGContextRef context = CGBitmapContextCreate(NULL, size.width * scale,
                                                     size.height * scale, 8, 0, colorSpace, kCGImageAlphaPremultipliedFirst);
        CGContextDrawImage(context,
                           CGRectMake(0, 0, size.width * scale, size.height * scale),
                           original.CGImage);
        CGImageRef shrunken = CGBitmapContextCreateImage(context);
        UIImage *final = [UIImageimageWithCGImage:shrunken];
       
        CGContextRelease(context);
        CGImageRelease(shrunken);
    
       
    
        return final;
    }
    - (void)updateDisplay {
        if ([lastChosenMediaTypeisEqual:(NSString *)kUTTypeImage]) {
            imageView.image = image;
            imageView.hidden = NO;
            moviePlayerController.view.hidden = YES;
        } elseif ([lastChosenMediaType isEqual:(NSString *)kUTTypeMovie]) {
            [self.moviePlayerController.view removeFromSuperview];
            self.moviePlayerController = [[MPMoviePlayerControlleralloc]
                                          initWithContentURL:movieURL];
            moviePlayerController.view.frame = imageFrame;
            moviePlayerController.view.clipsToBounds = YES;
            [self.viewaddSubview:moviePlayerController.view];
            imageView.hidden = YES;
        }
    }
    - (void)getMediaFromSource:(UIImagePickerControllerSourceType)sourceType {
        //判断设备是否具有该功能
        NSArray *mediaTypes = [UIImagePickerController
                               availableMediaTypesForSourceType:sourceType];
        if ([UIImagePickerControllerisSourceTypeAvailable:
             sourceType] && [mediaTypes count] > 0) {
            NSArray *mediaTypes = [UIImagePickerController
                                   availableMediaTypesForSourceType:sourceType];
            UIImagePickerController *picker =
            [[UIImagePickerController alloc] init];
            picker.mediaTypes = mediaTypes;
            picker.delegate = self;
            picker.allowsEditing = YES;
            picker.sourceType = sourceType;
            [self presentModalViewController:picker animated:YES];
        } else {
            //设备不支持该功能,弹出提示
            UIAlertView *alert = [[UIAlertView alloc]
                                  initWithTitle:@"Error accessing media"
                                  message:@"Device doesn’t support that media source."
                                  delegate:nil
                                  cancelButtonTitle:@"Drat!"
                                  otherButtonTitles:nil];
            [alert show];
        }
    }
  • 相关阅读:
    工厂方法模式
    命名空间的规划
    Download Manager
    Intent(二)
    Intent (一)
    Fragment (一)
    修改环境变量需不需要重启电脑
    Spring mvc和SSH如何取舍?
    ORA-01795: 列表中的最大表达式数为1000的解决方法详解
    apache与tomcate的区别
  • 原文地址:https://www.cnblogs.com/ios-wmm/p/10215626.html
Copyright © 2020-2023  润新知