当前位置:编程学习 > C/C++ >>

IOS研究院之打开照相机与本地相册选择图片

如下图所示 在本地相册中选择一张图片后,我们将他拷贝至沙盒当中,在客户端中将它的缩略图放在按钮旁边,这个结构其实和新浪微薄中选择图片后的效果一样。最终点击发送将按钮将图片2进制图片上传服务器。
 
 
下面我们仔细学习具体的细节。创建一个空的IOS项目,接着在创建一个ViewController。
AppDelegate.h 应用的代理类 这个没什么好说的就是直接打开刚刚创建的新ViewController。
1
#import <UIKit/UIKit.h>
2
#import "TestViewController.h"
3
 
4
@interface AppDelegate : UIResponder <UIApplicationDelegate>
5
 
6
@property (strong, nonatomic) UIWindow *window;
7
@property (strong, nonatomic) UINavigationController *navController;
8
@property (strong, nonatomic) UIViewController *viewController;
9
@end
 
AppDelegate.m 在这里就是打开我们创建的TestViewController
01
#import "AppDelegate.h"
02
 
03
@implementation AppDelegate
04
 
05
@synthesize window = _window;
06
@synthesize navController;
07
@synthesize viewController;
08
 
09
- (void)dealloc
10
{
11
    [_window release];
12
    [super dealloc];
13
}
14
 
15
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
16
{
17
    self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease];
18
 
19
    self.window.backgroundColor = [UIColor whiteColor];
20
    self.viewController =  [[TestViewController alloc]init];
21
    self.navController = [[UINavigationController alloc] initWithRootViewController:self.viewController];
22
    [self.window addSubview:navController.view];
23
 
24
    [self.window makeKeyAndVisible];
25
    return YES;
26
}
27
 
28
@end
 
TestViewController.h 注意这里面引入了很多代理类。
01
#import <UIKit/UIKit.h>
02
 
03
@interface TestViewController : UIViewController<UITextViewDelegate,UIActionSheetDelegate,UINavigationControllerDelegate,UIImagePickerControllerDelegate>
04
{
05
    //输入框
06
    UITextView *_textEditor;
07
 
08
    //下拉菜单
09
    UIActionSheet *myActionSheet;
10
 
11
    //图片2进制路径
12
    NSString* filePath;
13
}
14
@end
 
TestViewController.m 请大家仔细看这个类, 所有的东西都写在了这里哈。
001
#import "TestViewController.h"
002
 
003
@interface TestViewController ()
004
 
005
@end
006
 
007
@implementation TestViewController
008
 
009
- (void)viewDidLoad
010
{
011
    [super viewDidLoad];
012
    //导航栏标题
013
    self.navigationItem.title = @"雨松MOMO输入框";
014
 
015
    //导航栏按钮
016
    self.navigationItem.rightBarButtonItem = [[[UIBarButtonItem alloc]
017
                                               initWithTitle: @"发送"
018
                                               style: UIBarButtonItemStyleDone
019
                                               target: self
020
                                               action: @selector(sendInfo)] autorelease];
021
 
022
    //输入框显示区域
023
    _textEditor = [[UITextView alloc] initWithFrame:CGRectMake(0, 0, 320, 100)];
024
    //设置它的代理
025
    _textEditor.delegate = self;
026
    _textEditor.autoresizingMask = UIViewAutoresizingFlexibleWidth;
027
    _textEditor.keyboardType = UIKeyboardTypeDefault;
028
    _textEditor.font = [UIFont systemFontOfSize:20];
029
    _textEditor.text = @"请输入内容";
030
 
031
    //默认软键盘是在触摸区域后才会打开
032
    //这里表示进入当前ViewController直接打开软键盘
033
    [_textEditor becomeFirstResponder];
034
 
035
    //把输入框加在视图中
036
    [self.view addSubview:_textEditor];
037
 
038
    //下方的图片按钮 点击后呼出菜单 打开摄像机 查找本地相册
039
    UIImage *image = [[UIImage alloc] initWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"camera" ofType:@"png"]];
040
 
041
    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
042
    button.frame = CGRectMake(0, 120, image.size.width, image.size.height);
043
 
044
    [button setImage:image forState:UIControlStateNormal];
045
 
046
    [button addTarget:self action:@selector(openMenu) forControlEvents:UIControlEventTouchUpInside];
047
 
048
    //把它也加在视图当中
049
    [self.view addSubview:button];
050
 
051
}
052
 
053
-(void)openMenu
054
{
055
    //在这里呼出下方菜单按钮项
056
    myActionSheet = [[UIActionSheet alloc]
057
                 initWithTitle:nil
058
     
补充:软件开发 , C++ ,
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,