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
我们需要添加以下两个库
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
最后就实现效果了
分类:
IOS
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· 单线程的Redis速度为什么快?
· SQL Server 2025 AI相关能力初探
· 展开说说关于C#中ORM框架的用法!
· AI编程工具终极对决:字节Trae VS Cursor,谁才是开发者新宠?