FirstApp,iphone开发学习总结7,相机
生活随笔
收集整理的這篇文章主要介紹了
FirstApp,iphone开发学习总结7,相机
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
首先需要一個Nav,在FirstAppAppDelegate.m里添加:
-?(BOOL)application:(UIApplication?*)application?didFinishLaunchingWithOptions:(NSDictionary?*)launchOptions{
????//...
????ImageViewController?*imgTab?=?[[ImageViewController?alloc]?init];
????UINavigationController?*navImg?=?[[UINavigationController?alloc]?initWithRootViewController:imgTab];//添加
????
????NSArray?*tabArray?=?[[NSArray?alloc]?initWithObjects:navImg,?tableTab,?btnTab,?textTab,?navigation,?nil];//修改
????[navImg?release];//添加
????//...
}
進入ImageViewController.h文件,添加委托,并創建imageView變量:
@interface?ImageViewController?:?UIViewController<UIImagePickerControllerDelegate,?UINavigationControllerDelegate>{????UIImageView?*imageView;
}//UIImagePickerControllerDelegate繼承至UINavigationControllerDelegateImageViewController.m的- viewDidLoad略做修改,并添加相機按鈕://直接使用UIBarButtonSystemItemCamera了 -?(void)viewDidLoad
{
????imageView?=?[[UIImageView?alloc]?initWithFrame:CGRectMake(50.0,?50.0,?48.0,?48.0)];
????imageView.image?=?[UIImage?imageNamed:@"China.gif"];
????imageView.contentMode?=?UIViewContentModeScaleAspectFit;
????[self.view?addSubview:imageView];
????[imageView?release];
????????
????UIBarButtonItem?*imgRightBtn?=?[[UIBarButtonItem?alloc]?initWithBarButtonSystemItem:UIBarButtonSystemItemCamera?target:self?action:@selector(openCamera:)];
????[[self?navigationItem]?setRightBarButtonItem:imgRightBtn];
}
實現按鈕事件:
{
????UIImagePickerController?*imagePicker?=?[[UIImagePickerController?alloc]?init];//initWithRootViewController:
????if?([UIImagePickerController?isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera])?{
????????[imagePicker?setSourceType:UIImagePickerControllerSourceTypeCamera];
????}else?{
????????[imagePicker?setSourceType:UIImagePickerControllerSourceTypePhotoLibrary];
????}
????[imagePicker?setDelegate:self];
????[self?presentModalViewController:imagePicker?animated:YES];
????[imagePicker?release];
}
?此處代碼:
? ?? isSourceTypeAvailable判定此設備是否支持相機,不然從圖片目錄獲取圖片。
? ?? UIImagePickerController實例以模態展示,需要presentModalViewController它。
? ?? souceType分別代表:Camera(打開相機),PhotoLibrary(打開相冊),SavedPhotoAlbums(最近拍攝)
?
圖片選擇完成,則觸發委托事件:
{
????UIImage?*image?=?[info?objectForKey:UIImagePickerControllerOriginalImage];
????[imageView?setImage:image];
????
????[self?dismissModalViewControllerAnimated:YES];
}
?如果關閉了界面,則取消操作,觸發:
imagePickerControllerDidCancel:可以工作了,此處的一個問題就是當拍攝圖片過大,內存警告回收資源,圖片則不會顯示。通過保存圖片等操作解決,后面說。
?
求指點!
轉載于:https://www.cnblogs.com/maxfong/archive/2012/05/04/2482847.html
總結
以上是生活随笔為你收集整理的FirstApp,iphone开发学习总结7,相机的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: HDU-1978 How many wa
- 下一篇: Java Socket入门实例