• IOS开发---菜鸟学习之路--(十五)-如何实现拍照功能


    本章将来讲解下如何实现拍照的功能

    我们需要的实现的效果是 

        

    好了 直接开始内容吧

    首先我们需要新建一个ViewController

    就叫AddPictureViewController

    然后选择.h文件进行如下修改

     1 #import <UIKit/UIKit.h>
     2 
     3 @interface AddPictureViewController : UIViewController<UIImagePickerControllerDelegate,UINavigationControllerDelegate,UIAlertViewDelegate>
     4 {
     5     UITextView *contenttextview;
     6     UIImageView *contentimageview;
     7     NSString *lastChosenMediaType;
     8 
     9 }
    10 @property(nonatomic,retain) IBOutlet UITextView *contenttextview;
    11 @property(nonatomic,retain)  IBOutlet   UIImageView *contentimageview;
    12 @property(nonatomic,copy)        NSString *lastChosenMediaType;
    13 -(IBAction)buttonclick:(id)sender;
    14 
    15 @end
    AddPictureViewController.h

    我们需要添加以下两个库

    QuartzCore

    MobileCoreServices

    在项目的General里找到  linked Frameworks and libraries 添加两个类库

    然后修改XIB文件

    并建立相关链接

    最后就是.m文件部分的代码了。

    我就直接上代码了 大家可以直接复制过去 然后再理解

      1 #import "AddPictureViewController.h"
      2 #import <QuartzCore/QuartzCore.h>
      3 #import <QuartzCore/CoreAnimation.h>
      4 #import <MobileCoreServices/UTCoreTypes.h>
      5 @interface AddPictureViewController ()
      6 
      7 @end
      8 
      9 @implementation AddPictureViewController
     10 @synthesize contentimageview;
     11 @synthesize contenttextview;
     12 @synthesize lastChosenMediaType;
     13 - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
     14 {
     15     self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
     16     if (self) {
     17         // Custom initialization
     18     }
     19     return self;
     20 }
     21 
     22 - (void)viewDidLoad
     23 {
     24     [super viewDidLoad];
     25     // Do any additional setup after loading the view from its nib.
     26 }
     27 -(IBAction)buttonclick:(id)sender
     28 {
     29     UIAlertView *alert=[[UIAlertView alloc] initWithTitle:@"请选择图片来源" message:@"" delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"拍照",@"从手机相册选择", nil];
     30     [alert show];
     31 }
     32 - (void)didReceiveMemoryWarning
     33 {
     34     [super didReceiveMemoryWarning];
     35     // Dispose of any resources that can be recreated.
     36 }
     37 #pragma 拍照选择模块
     38 -(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{
     39     if(buttonIndex==1)
     40         [self shootPiicturePrVideo];
     41     else if(buttonIndex==2)
     42         [self selectExistingPictureOrVideo];
     43 }
     44 #pragma  mark- 拍照模块
     45 //从相机上选择
     46 -(void)shootPiicturePrVideo{
     47     [self getMediaFromSource:UIImagePickerControllerSourceTypeCamera];
     48 }
     49 //从相册中选择
     50 -(void)selectExistingPictureOrVideo{
     51     [self getMediaFromSource:UIImagePickerControllerSourceTypePhotoLibrary];
     52 }
     53 #pragma 拍照模块
     54 -(void) imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info{
     55     self.lastChosenMediaType=[info objectForKey:UIImagePickerControllerMediaType];
     56     if([lastChosenMediaType isEqual:(NSString *) kUTTypeImage])
     57     {
     58         UIImage *chosenImage=[info objectForKey:UIImagePickerControllerEditedImage];
     59         contentimageview.image=chosenImage;
     60     }
     61     if([lastChosenMediaType isEqual:(NSString *) kUTTypeMovie])
     62     {
     63         UIAlertView *alert=[[UIAlertView alloc] initWithTitle:@"提示信息!" message:@"系统只支持图片格式" delegate:nil cancelButtonTitle:@"确认" otherButtonTitles: nil];
     64         [alert show];
     65         
     66     }
     67     [picker dismissModalViewControllerAnimated:YES];
     68 }
     69 -(void) imagePickerControllerDidCancel:(UIImagePickerController *)picker{
     70     [picker dismissModalViewControllerAnimated:YES];
     71 }
     72 -(void)getMediaFromSource:(UIImagePickerControllerSourceType)sourceType
     73 {
     74     NSArray *mediatypes=[UIImagePickerController availableMediaTypesForSourceType:sourceType];
     75     if([UIImagePickerController isSourceTypeAvailable:sourceType] &&[mediatypes count]>0){
     76         NSArray *mediatypes=[UIImagePickerController availableMediaTypesForSourceType:sourceType];
     77         UIImagePickerController *picker=[[UIImagePickerController alloc] init];
     78         picker.mediaTypes=mediatypes;
     79         picker.delegate=self;
     80         picker.allowsEditing=YES;
     81         picker.sourceType=sourceType;
     82         NSString *requiredmediatype=(NSString *)kUTTypeImage;
     83         NSArray *arrmediatypes=[NSArray arrayWithObject:requiredmediatype];
     84         [picker setMediaTypes:arrmediatypes];
     85         [self presentModalViewController:picker animated:YES];
     86     }
     87     else{
     88         UIAlertView *alert=[[UIAlertView alloc] initWithTitle:@"错误信息!" message:@"当前设备不支持拍摄功能" delegate:nil cancelButtonTitle:@"确认" otherButtonTitles: nil];
     89         [alert show];
     90     }
     91 }
     92 static UIImage *shrinkImage(UIImage *orignal,CGSize size)
     93 {
     94     CGFloat scale=[UIScreen mainScreen].scale;
     95     CGColorSpaceRef colorSpace=CGColorSpaceCreateDeviceRGB();
     96     CGContextRef context=CGBitmapContextCreate(NULL, size.width *scale,size.height*scale, 8, 0, colorSpace, kCGImageAlphaPremultipliedFirst);
     97     CGContextDrawImage(context, CGRectMake(0, 0, size.width*scale, size.height*scale), orignal.CGImage);
     98     CGImageRef shrunken=CGBitmapContextCreateImage(context);
     99     UIImage *final=[UIImage imageWithCGImage:shrunken];
    100     CGContextRelease(context);
    101     CGImageRelease(shrunken);
    102     return  final;
    103 }
    104 
    105 @end
    AddPictureViewController.m

    最后就实现效果了

  • 相关阅读:
    Php排序
    php导出excel表
    yii的多表查询
    MySQL中自定义排序
    jquery中的obj.attr()和obj.data
    PhpStorm快捷键
    记2019年目标之一没有996的大数据分析BI实战历程
    2018年传统公司技术部门技术变迁和2019展望
    mpvue微信小程序开发随笔
    docker toolbox的redis 配置主从及哨兵模式保证高可用
  • 原文地址:https://www.cnblogs.com/PleaseInputEnglish/p/3486559.html
Copyright © 2020-2023  润新知