• IOS照相


    #import <UIKit/UIKit.h>

    @interface AddPictureViewController : UIViewController<UIImagePickerControllerDelegate,UINavigationControllerDelegate,UIAlertViewDelegate>
    {
    UITextView *contenttextview;
    UIImageView *contentimageview;
    NSString *lastChosenMediaType;

    }
    @property(nonatomic,retain) IBOutlet UITextView *contenttextview;
    @property(nonatomic,retain) IBOutlet UIImageView *contentimageview;
    @property(nonatomic,copy) NSString *lastChosenMediaType;
    -(IBAction)buttonclick:(id)sender;

    @end

    AddPictureViewController.h

    我们需要添加以下两个库

    QuartzCore

    MobileCoreServices

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

    然后修改XIB文件

    #import "AddPictureViewController.h"
    #import <QuartzCore/QuartzCore.h>
    #import <QuartzCore/CoreAnimation.h>
    #import <MobileCoreServices/UTCoreTypes.h>
    @interface AddPictureViewController ()

    @end

    @implementation AddPictureViewController
    @synthesize contentimageview;
    @synthesize contenttextview;
    @synthesize lastChosenMediaType;
    - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
    {
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
    // Custom initialization
    }
    return self;
    }

    - (void)viewDidLoad
    {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    }
    -(IBAction)buttonclick:(id)sender
    {
    UIAlertView *alert=[[UIAlertView alloc] initWithTitle:@"请选择图片来源" message:@"" delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"拍照",@"从手机相册选择", nil];
    [alert show];
    }
    - (void)didReceiveMemoryWarning
    {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
    }
    #pragma 拍照选择模块
    -(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{
    if(buttonIndex==1)
    [self shootPiicturePrVideo];
    else if(buttonIndex==2)
    [self selectExistingPictureOrVideo];
    }
    #pragma mark- 拍照模块
    //从相机上选择
    -(void)shootPiicturePrVideo{
    [self getMediaFromSource:UIImagePickerControllerSourceTypeCamera];
    }
    //从相册中选择
    -(void)selectExistingPictureOrVideo{
    [self getMediaFromSource:UIImagePickerControllerSourceTypePhotoLibrary];
    }
    #pragma 拍照模块
    -(void) imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info{
    self.lastChosenMediaType=[info objectForKey:UIImagePickerControllerMediaType];
    if([lastChosenMediaType isEqual:(NSString *) kUTTypeImage])
    {
    UIImage *chosenImage=[info objectForKey:UIImagePickerControllerEditedImage];
    contentimageview.image=chosenImage;
    }
    if([lastChosenMediaType isEqual:(NSString *) kUTTypeMovie])
    {
    UIAlertView *alert=[[UIAlertView alloc] initWithTitle:@"提示信息!" message:@"系统只支持图片格式" delegate:nil cancelButtonTitle:@"确认" otherButtonTitles: nil];
    [alert show];

    }
    [picker dismissModalViewControllerAnimated:YES];
    }
    -(void) imagePickerControllerDidCancel:(UIImagePickerController *)picker{
    [picker dismissModalViewControllerAnimated:YES];
    }
    -(void)getMediaFromSource:(UIImagePickerControllerSourceType)sourceType
    {
    NSArray *mediatypes=[UIImagePickerController availableMediaTypesForSourceType:sourceType];
    if([UIImagePickerController isSourceTypeAvailable: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;
    NSString *requiredmediatype=(NSString *)kUTTypeImage;
    NSArray *arrmediatypes=[NSArray arrayWithObject:requiredmediatype];
    [picker setMediaTypes:arrmediatypes];
    [self presentModalViewController:picker animated:YES];
    }
    else{
    UIAlertView *alert=[[UIAlertView alloc] initWithTitle:@"错误信息!" message:@"当前设备不支持拍摄功能" delegate:nil cancelButtonTitle:@"确认" otherButtonTitles: nil];
    [alert show];
    }
    }
    static UIImage *shrinkImage(UIImage *orignal,CGSize size)
    {
    CGFloat scale=[UIScreen mainScreen].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), orignal.CGImage);
    CGImageRef shrunken=CGBitmapContextCreateImage(context);
    UIImage *final=[UIImage imageWithCGImage:shrunken];
    CGContextRelease(context);
    CGImageRelease(shrunken);
    return final;
    }

    @end

    AddPictureViewController.m

  • 相关阅读:
    C++ 抽象类二(抽象类的基本语法)
    C++ 抽象类一(多继承与赋值兼容性原则)
    C++ 类的多态五(多态的语法本质分析)
    C++ 类的多态四(虚析构函数的重要性)
    C++ 类的多态三(多态的原理--虚函数指针--子类虚函数指针初始化)
    MEF等Ioc框架引起内存泄露-PartCreationPolicy
    MEF部件的生命周期(PartCreationPolicy)
    wpf prism IRegionManager 和IRegionViewRegistry
    silverlight开发实例(Prism+MVVM+RIA)(二)--创建shell及用户登录
    Prism框架 如何在主程序中合理的弹出子窗体
  • 原文地址:https://www.cnblogs.com/wcLT/p/4744119.html
Copyright © 2020-2023  润新知