UIImagePickerController在iOS6.0 的iPad上,不能只用在橫向的設計上,會導致Crash,其原因可以看
https://devforums.apple.com/message/731764#731764
裡面有詳細的說明,這的錯誤點是iOS6.0 SDK的原生錯誤,而此錯誤只會出現在不要旋轉模式的橫向App。以下是解決方法。
1. 沿用前一篇UIImagePickerController的例子,將方向設成橫向
2. 加入新的class檔,這是為了要避開原生的UIImagePickerController錯誤。
3. noRotationImagePicker.m上加入避開錯誤的程式碼
#import "noRotationImagePicker.h"
@interface noRotationImagePicker ()
@end
@implementation noRotationImagePicker
- (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.
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (BOOL)shouldAutorotate
{
return NO;
}
-(UIInterfaceOrientation)preferredInterfaceOrientationForPresentation
{
return UIInterfaceOrientationPortrait;
}
4. 在mainViewController.m上使用的地方,改成呼叫自己所改的Class
- (IBAction)openPhotoLibrary:(id)sender {
//UIView *anchor = sender;
UIImagePickerController *m_imagePicker = [[noRotationImagePicker alloc] init];
if ([UIImagePickerController isSourceTypeAvailable:
UIImagePickerControllerSourceTypePhotoLibrary]) {
m_imagePicker.wantsFullScreenLayout = YES;
m_imagePicker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
m_imagePicker.delegate = self;
[m_imagePicker setAllowsEditing:YES]; // 圖片出現可編輯的選項
popover = [[UIPopoverController alloc] initWithContentViewController:m_imagePicker];
[popover setPopoverContentSize:CGSizeMake(300, 300)];
[popover presentPopoverFromRect:CGRectMake(0,0,300,300) inView:self.view permittedArrowDirections:UIPopoverArrowDirectionAny animated:YES]; // 此處popover的位置改成自定
}
}
- (IBAction)openPhotoAlbum:(id)sender {
UIView *anchor = sender;
UIImagePickerController *m_imagePicker = [[noRotationImagePicker alloc] init];
if ([UIImagePickerController isSourceTypeAvailable:
UIImagePickerControllerSourceTypeSavedPhotosAlbum]) {
m_imagePicker.sourceType = UIImagePickerControllerSourceTypeSavedPhotosAlbum;
m_imagePicker.delegate = self;
[m_imagePicker setAllowsEditing:YES];
popover = [[UIPopoverController alloc] initWithContentViewController:m_imagePicker];
[popover setPopoverContentSize:CGSizeMake(300, 300)];
[popover presentPopoverFromRect:anchor.frame inView:anchor.superview permittedArrowDirections:UIPopoverArrowDirectionAny animated:YES];
}
}
- (void) imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
UIImage *image = [info objectForKey:UIImagePickerControllerOriginalImage];
[imageView setImage:image];
[popover dismissPopoverAnimated:YES]; // 使用正確的popover dismiss
}
2013年1月19日 星期六
2013年1月18日 星期五
UIImagePickerController的使用(三)
延續前一篇,當我取得了一個圖檔之後,除了顯示出來,也希望可以儲存起來,以下就加入儲存與讀取的機制。儲存位置在App的Document區。
1. 在Storyboard加上三個Button
2. 在mainViewController.m加上所需程式碼
- (IBAction)saveScreen:(id)sender {
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *savedImagePath = [documentsDirectory stringByAppendingPathComponent:@"savedImage.png"];
NSData *imageData = UIImagePNGRepresentation(imageView.image);
[imageData writeToFile:savedImagePath atomically:YES];
}
- (IBAction)clearScreen:(id)sender {
[imageView setImage:nil];
}
- (IBAction)readPNG:(id)sender {
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
//NSString *ImagePath = [documentsDirectory stringByAppendingPathComponent:@"savedImage.png"];
NSURL *recordedFile = [NSURL fileURLWithPath:[documentsDirectory stringByAppendingString:@"/savedImage.png"]];
UIImage *image1 = [UIImage imageWithData:[NSData dataWithContentsOfURL:recordedFile]];
imageView.image = image1;
}
1. 在Storyboard加上三個Button
2. 在mainViewController.m加上所需程式碼
- (IBAction)saveScreen:(id)sender {
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *savedImagePath = [documentsDirectory stringByAppendingPathComponent:@"savedImage.png"];
NSData *imageData = UIImagePNGRepresentation(imageView.image);
[imageData writeToFile:savedImagePath atomically:YES];
}
- (IBAction)clearScreen:(id)sender {
[imageView setImage:nil];
}
- (IBAction)readPNG:(id)sender {
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
//NSString *ImagePath = [documentsDirectory stringByAppendingPathComponent:@"savedImage.png"];
NSURL *recordedFile = [NSURL fileURLWithPath:[documentsDirectory stringByAppendingString:@"/savedImage.png"]];
UIImage *image1 = [UIImage imageWithData:[NSData dataWithContentsOfURL:recordedFile]];
imageView.image = image1;
}
2013年1月17日 星期四
UIToolBar的基本使用(四)
延續前一篇的實驗,使Toolbar及BarItem的字體放大
實作如下
1. 只要在dynamicToolBarViewController.m上修改程式碼即可
-(void)setupMyToolBar
{
myToolBar = [[UIToolbar alloc] initWithFrame:CGRectMake(0, 682, 1024, 66)];
myToolBar.barStyle = UIBarStyleBlackTranslucent;
UIFont *font = [UIFont boldSystemFontOfSize:32];//改變字體大小
NSDictionary *attribute = [NSDictionary dictionaryWithObject:font forKey:UITextAttributeFont];
//myBarItem1= [[UIBarButtonItem alloc] initWithCustomView:label];
myBarItem1= [[UIBarButtonItem alloc]initWithTitle:@"Test1" style:UIBarButtonItemStylePlain target:self action:@selector(showText1)];
myBarItem2= [[UIBarButtonItem alloc]initWithBarButtonSystemItem:(UIBarButtonSystemItemFixedSpace) target:nil action:nil];
myBarItem3= [[UIBarButtonItem alloc]initWithTitle:@"Test2" style:UIBarButtonItemStyleBordered target:self action:@selector(showText2)];
myBarItem4= [[UIBarButtonItem alloc]initWithBarButtonSystemItem:(UIBarButtonSystemItemFlexibleSpace) target:nil action:nil];
myBarItem5= [[UIBarButtonItem alloc]initWithTitle:@"Test3" style:UIBarButtonItemStyleDone target:self action:@selector(showText3)];
myBarItem2.width = 360;
[myBarItem1 setTitleTextAttributes:attribute forState:UIControlStateNormal];
[myBarItem3 setTitleTextAttributes:attribute forState:UIControlStateNormal];
[myBarItem5 setTitleTextAttributes:attribute forState:UIControlStateNormal];
NSArray *toolbarItems = [[NSArray alloc] initWithObjects:myBarItem1,myBarItem2,myBarItem3,myBarItem4,myBarItem5,nil];
myToolBar.items = toolbarItems;
}
2. 顯示效果
實作如下
1. 只要在dynamicToolBarViewController.m上修改程式碼即可
-(void)setupMyToolBar
{
myToolBar = [[UIToolbar alloc] initWithFrame:CGRectMake(0, 682, 1024, 66)];
myToolBar.barStyle = UIBarStyleBlackTranslucent;
UIFont *font = [UIFont boldSystemFontOfSize:32];//改變字體大小
NSDictionary *attribute = [NSDictionary dictionaryWithObject:font forKey:UITextAttributeFont];
//myBarItem1= [[UIBarButtonItem alloc] initWithCustomView:label];
myBarItem1= [[UIBarButtonItem alloc]initWithTitle:@"Test1" style:UIBarButtonItemStylePlain target:self action:@selector(showText1)];
myBarItem2= [[UIBarButtonItem alloc]initWithBarButtonSystemItem:(UIBarButtonSystemItemFixedSpace) target:nil action:nil];
myBarItem3= [[UIBarButtonItem alloc]initWithTitle:@"Test2" style:UIBarButtonItemStyleBordered target:self action:@selector(showText2)];
myBarItem4= [[UIBarButtonItem alloc]initWithBarButtonSystemItem:(UIBarButtonSystemItemFlexibleSpace) target:nil action:nil];
myBarItem5= [[UIBarButtonItem alloc]initWithTitle:@"Test3" style:UIBarButtonItemStyleDone target:self action:@selector(showText3)];
myBarItem2.width = 360;
[myBarItem1 setTitleTextAttributes:attribute forState:UIControlStateNormal];
[myBarItem3 setTitleTextAttributes:attribute forState:UIControlStateNormal];
[myBarItem5 setTitleTextAttributes:attribute forState:UIControlStateNormal];
NSArray *toolbarItems = [[NSArray alloc] initWithObjects:myBarItem1,myBarItem2,myBarItem3,myBarItem4,myBarItem5,nil];
myToolBar.items = toolbarItems;
}
2. 顯示效果
2013年1月16日 星期三
UIAlertView的基本使用(六)
有時我們需要在alterView上放置圖片,來清楚地標示些資訊,以下是實作方式
根據前一篇的例子,加上兩張圖片來做顯示
很簡當,只需在alter上加入UIImageView就可以了
- (void) alertViewAction1
{
UIAlertView *alert1 = [[UIAlertView alloc] initWithTitle:@"Title" message:@"Message 1......\nMessage 2......" delegate:self cancelButtonTitle:@"OK" otherButtonTitles:@"Button 1", @"Button 2", @"Button 3", nil];
UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(180, 10, 100, 100)];
NSString *path = [[NSString alloc] initWithString:[[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:@"1001.png"]];
UIImage *bkgImg = [[UIImage alloc] initWithContentsOfFile:path];
[imageView setImage:bkgImg];
[alert1 addSubview:imageView];
UIImageView *imageView2 = [[UIImageView alloc] initWithFrame:CGRectMake(10, 10, 100, 100)];
NSString *path2 = [[NSString alloc] initWithString:[[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:@"1002.png"]];
UIImage *bkgImg2 = [[UIImage alloc] initWithContentsOfFile:path2];
[imageView2 setImage:bkgImg2];
[alert1 addSubview:imageView2];
[alert1 show];
}
根據前一篇的例子,加上兩張圖片來做顯示
很簡當,只需在alter上加入UIImageView就可以了
- (void) alertViewAction1
{
UIAlertView *alert1 = [[UIAlertView alloc] initWithTitle:@"Title" message:@"Message 1......\nMessage 2......" delegate:self cancelButtonTitle:@"OK" otherButtonTitles:@"Button 1", @"Button 2", @"Button 3", nil];
UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(180, 10, 100, 100)];
NSString *path = [[NSString alloc] initWithString:[[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:@"1001.png"]];
UIImage *bkgImg = [[UIImage alloc] initWithContentsOfFile:path];
[imageView setImage:bkgImg];
[alert1 addSubview:imageView];
UIImageView *imageView2 = [[UIImageView alloc] initWithFrame:CGRectMake(10, 10, 100, 100)];
NSString *path2 = [[NSString alloc] initWithString:[[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:@"1002.png"]];
UIImage *bkgImg2 = [[UIImage alloc] initWithContentsOfFile:path2];
[imageView2 setImage:bkgImg2];
[alert1 addSubview:imageView2];
[alert1 show];
}
2013年1月15日 星期二
UINavigationBar的基本使用(二)
延續前一篇 UINavigationBar的基本使用(一) 的專案,加上一個動態popover的使用,主要語法如下
[popover presentPopoverFromRect:CGRectMake(0, 0, 2000, 50) inView:super.view permittedArrowDirections:UIPopoverArrowDirectionUp animated:YES];
presentPopoverFromRect:設定popover在iPAD上起始的位置點
1. 在dynamicNavBarViewController. m加入所需程式碼
-(void)myNavAction1
{
NSString *text = @"測試左鍵";
showLabel.text = text;
[self popoverAction1];
}
-(void)myNavAction2
{
NSString *text = @"測試右鍵";
showLabel.text = text;
[self popoverAction2];
}
-(void)myNavAction3
{
NSString *text = @"測試右二鍵";
showLabel.text = text;
[self popoverAction3];
}
- (void) popoverAction1 {
SecondView *showView1 = [[SecondView alloc] init];
showView1.view.backgroundColor = [UIColor redColor];
UILabel *testLabel =[[UILabel alloc] initWithFrame:CGRectMake(0, 0, 150, 150)];
testLabel.backgroundColor = [UIColor clearColor];
testLabel.textColor = [UIColor blackColor];
testLabel.text = @"左鍵測試";
[showView1.view addSubview:testLabel ];
testLabel.Font = [UIFont boldSystemFontOfSize:25];
popover = [[UIPopoverController alloc]
initWithContentViewController:showView1];
[popover setPopoverContentSize:CGSizeMake(300, 300)];
[popover presentPopoverFromRect:CGRectMake(0, 0, 50, 50) inView:super.view permittedArrowDirections:UIPopoverArrowDirectionUp animated:YES];
}
- (void) popoverAction2 {
SecondView *showView1 = [[SecondView alloc] init];
showView1.view.backgroundColor = [UIColor redColor];
UILabel *testLabel =[[UILabel alloc] initWithFrame:CGRectMake(0, 0, 150, 150)];
testLabel.backgroundColor = [UIColor clearColor];
testLabel.textColor = [UIColor blackColor];
testLabel.text = @"右鍵測試";
testLabel.Font = [UIFont boldSystemFontOfSize:25];
[showView1.view addSubview:testLabel ];
popover = [[UIPopoverController alloc]
initWithContentViewController:showView1];
[popover setPopoverContentSize:CGSizeMake(300, 300)];
[popover presentPopoverFromRect:CGRectMake(0, 0, 2000, 50) inView:super.view permittedArrowDirections:UIPopoverArrowDirectionUp animated:YES];
}
- (void) popoverAction3 {
SecondView *showView1 = [[SecondView alloc] init];
showView1.view.backgroundColor = [UIColor redColor];
UILabel *testLabel =[[UILabel alloc] initWithFrame:CGRectMake(0, 0, 150, 150)];
testLabel.backgroundColor = [UIColor clearColor];
testLabel.textColor = [UIColor blackColor];
testLabel.Font = [UIFont boldSystemFontOfSize:25];
testLabel.text = @"右二鍵測試";
[showView1.view addSubview:testLabel ];
popover = [[UIPopoverController alloc]
initWithContentViewController:showView1];
[popover setPopoverContentSize:CGSizeMake(300, 300)];
[popover presentPopoverFromRect:CGRectMake(0, 0, 1850, 50) inView:super.view permittedArrowDirections:UIPopoverArrowDirectionUp animated:YES];
}
2. 顯示結果
[popover presentPopoverFromRect:CGRectMake(0, 0, 2000, 50) inView:super.view permittedArrowDirections:UIPopoverArrowDirectionUp animated:YES];
presentPopoverFromRect:設定popover在iPAD上起始的位置點
1. 在dynamicNavBarViewController. m加入所需程式碼
-(void)myNavAction1
{
NSString *text = @"測試左鍵";
showLabel.text = text;
[self popoverAction1];
}
-(void)myNavAction2
{
NSString *text = @"測試右鍵";
showLabel.text = text;
[self popoverAction2];
}
-(void)myNavAction3
{
NSString *text = @"測試右二鍵";
showLabel.text = text;
[self popoverAction3];
}
- (void) popoverAction1 {
SecondView *showView1 = [[SecondView alloc] init];
showView1.view.backgroundColor = [UIColor redColor];
UILabel *testLabel =[[UILabel alloc] initWithFrame:CGRectMake(0, 0, 150, 150)];
testLabel.backgroundColor = [UIColor clearColor];
testLabel.textColor = [UIColor blackColor];
testLabel.text = @"左鍵測試";
[showView1.view addSubview:testLabel ];
testLabel.Font = [UIFont boldSystemFontOfSize:25];
popover = [[UIPopoverController alloc]
initWithContentViewController:showView1];
[popover setPopoverContentSize:CGSizeMake(300, 300)];
[popover presentPopoverFromRect:CGRectMake(0, 0, 50, 50) inView:super.view permittedArrowDirections:UIPopoverArrowDirectionUp animated:YES];
}
- (void) popoverAction2 {
SecondView *showView1 = [[SecondView alloc] init];
showView1.view.backgroundColor = [UIColor redColor];
UILabel *testLabel =[[UILabel alloc] initWithFrame:CGRectMake(0, 0, 150, 150)];
testLabel.backgroundColor = [UIColor clearColor];
testLabel.textColor = [UIColor blackColor];
testLabel.text = @"右鍵測試";
testLabel.Font = [UIFont boldSystemFontOfSize:25];
[showView1.view addSubview:testLabel ];
popover = [[UIPopoverController alloc]
initWithContentViewController:showView1];
[popover setPopoverContentSize:CGSizeMake(300, 300)];
[popover presentPopoverFromRect:CGRectMake(0, 0, 2000, 50) inView:super.view permittedArrowDirections:UIPopoverArrowDirectionUp animated:YES];
}
- (void) popoverAction3 {
SecondView *showView1 = [[SecondView alloc] init];
showView1.view.backgroundColor = [UIColor redColor];
UILabel *testLabel =[[UILabel alloc] initWithFrame:CGRectMake(0, 0, 150, 150)];
testLabel.backgroundColor = [UIColor clearColor];
testLabel.textColor = [UIColor blackColor];
testLabel.Font = [UIFont boldSystemFontOfSize:25];
testLabel.text = @"右二鍵測試";
[showView1.view addSubview:testLabel ];
popover = [[UIPopoverController alloc]
initWithContentViewController:showView1];
[popover setPopoverContentSize:CGSizeMake(300, 300)];
[popover presentPopoverFromRect:CGRectMake(0, 0, 1850, 50) inView:super.view permittedArrowDirections:UIPopoverArrowDirectionUp animated:YES];
}
2. 顯示結果
2013年1月14日 星期一
xcode 的Quick Help
在寫App時經常會卡在一些小細節,Xcode提供了很好的協助機制,就是Quick Help。
1. 例如如果臨時遇到 popover的問題如圖,只要用鼠標將特殊字標起來變成藍色,右邊的Quick Help,就會出現相關的說明資料。
2. 如果還是不清楚,就在Quick Help向下滑動,最下面通常會有範例,如圖,可以看到popovers的例子。
3. 點擊popovers,會出現例子的說明資料,除了Next可以仔細地瀏覽細節外,最重要的是在Open Project,點擊後,會直接在Xcode上開啓這個例子。
4. 開啓例子後,直接就可以執行及追蹤程式碼。這樣就可以很快地瞭解該功能的使用了。
1. 例如如果臨時遇到 popover的問題如圖,只要用鼠標將特殊字標起來變成藍色,右邊的Quick Help,就會出現相關的說明資料。
2. 如果還是不清楚,就在Quick Help向下滑動,最下面通常會有範例,如圖,可以看到popovers的例子。
3. 點擊popovers,會出現例子的說明資料,除了Next可以仔細地瀏覽細節外,最重要的是在Open Project,點擊後,會直接在Xcode上開啓這個例子。
4. 開啓例子後,直接就可以執行及追蹤程式碼。這樣就可以很快地瞭解該功能的使用了。
2013年1月13日 星期日
Navigation Controller實作(二)
延續前一篇 Navigation Controller實作(一)
要在Navigation Bar上加入title的顯示
修改如下
1.在AppViewController.m加上顯示程式碼
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
[self setTitle:@"Main Page"];
}
- (IBAction)toPage2:(id)sender {
SecondViewController *svc = [self.storyboard instantiateViewControllerWithIdentifier:@"SecondId"];
[svc setValue:@"Page2" forKey:@"transferString"];
[self.navigationController pushViewController:svc animated:YES];
}
2. 在SecondViewController.h加上傳送用的字串變數。
#import <UIKit/UIKit.h>
@interface SecondViewController : UIViewController
{
NSString *transferString;
}
@end
3. 在SecondViewController.m加上顯示title的程式碼
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view.
self.title=transferString;
}
4. 顯示結果
要在Navigation Bar上加入title的顯示
修改如下
1.在AppViewController.m加上顯示程式碼
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
[self setTitle:@"Main Page"];
}
- (IBAction)toPage2:(id)sender {
SecondViewController *svc = [self.storyboard instantiateViewControllerWithIdentifier:@"SecondId"];
[svc setValue:@"Page2" forKey:@"transferString"];
[self.navigationController pushViewController:svc animated:YES];
}
2. 在SecondViewController.h加上傳送用的字串變數。
#import <UIKit/UIKit.h>
@interface SecondViewController : UIViewController
{
NSString *transferString;
}
@end
3. 在SecondViewController.m加上顯示title的程式碼
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view.
self.title=transferString;
}
4. 顯示結果
訂閱:
文章 (Atom)













