IOS笔记汇集

1.iOS调用相册和摄像头

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    UIImageView *imageView = [[UIImageView alloc] init];
    imageView.frame = CGRectMake(0, 0, 80, 120);
    imageView.backgroundColor = [UIColor greenColor];
    imageView.tag = 101;
    
    [self.view addSubview:imageView];
    
    UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    button.frame = CGRectMake(0, 200, 100, 30);
    [button setTitle:@"打开相册" forState:UIControlStateNormal];
    [button addTarget:self action:@selector(openPics) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:button];
    
    UIButton *button2 = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    button2.frame = CGRectMake(0, 300, 100, 30);
    [button2 setTitle:@"打开相机" forState:UIControlStateNormal];
    [button2 addTarget:self action:@selector(openCamera) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:button2];
}

// 打开相机
- (void)openCamera {
    // UIImagePickerControllerCameraDeviceRear 后置摄像头
    // UIImagePickerControllerCameraDeviceFront 前置摄像头
    BOOL isCamera = [UIImagePickerController isCameraDeviceAvailable:UIImagePickerControllerCameraDeviceRear];
    if (!isCamera) {
        NSLog(@"没有摄像头");
        return ;
    }
    
    UIImagePickerController *imagePicker = [[UIImagePickerController alloc] init];
    imagePicker.sourceType = UIImagePickerControllerSourceTypeCamera;
    imagePicker.delegate = self;
    // 编辑模式
    imagePicker.allowsEditing = YES;
    
    [self  presentViewController:imagePicker animated:YES completion:^{
    }];

    
}


// 打开相册
- (void)openPics {

    UIImagePickerController *imagePicker = [[UIImagePickerController alloc] init];
    imagePicker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
    imagePicker.delegate = self;
    [self  presentViewController:imagePicker animated:YES completion:^{
    }];
    
    
}


// 选中照片

- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info{
    NSLog(@"%@", info);
    UIImageView  *imageView = (UIImageView *)[self.view viewWithTag:101];
    // UIImagePickerControllerOriginalImage 原始图片
    // UIImagePickerControllerEditedImage 编辑后图片
    UIImage *image = [info objectForKey:UIImagePickerControllerEditedImage];
    imageView.image = image;
    [picker dismissViewControllerAnimated:YES completion:NULL];
    
}



// 取消相册
- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker {
    [picker dismissViewControllerAnimated:YES completion:NULL];

}


2.ASIHttpRequest框架使用

要使用ASIRequest必须添加5个动态库,CFNetwork.framework、SystemConfigureation.framework、MobileCoreServices.framework、libz.dylib和libxml2.dylib


ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:url];
[request setRequestMethod:@"GET"];
[request setTimeOutSeconds:60];
// 设置请求头
// [request setRequestHeaders:<#(NSMutableDictionary *)#>]
// 设置cookies
// [request setRequestCookies:<#(NSMutableArray *)#>]
// 发送同步请求
[request startSynchronous];
NSError *error = request.error;
if (error == nil) {
    NSData *data = request.responseData;
    UIImage  *img = [UIImage imageWithData:data];
    NSLog(@"%@", data);
    self.image = img;
} else {
    NSLog(@"请求网络出错:%@", error);
}


3.iOS Http请求异步请求

- (void)viewDidLoad
{
    [super viewDidLoad];
	// Do any additional setup after loading the view.
    
    _data = [[NSMutableData alloc] init];
    
    // 组合一个搜索字符串
    NSString *urlStr = [NSString stringWithFormat:@"http://www.baidu.com/s?wd=%@", @"php"];
    NSURL *url = [NSURL URLWithString:urlStr];
    
    NSURLRequest *request = [NSURLRequest requestWithURL:url];
    
    //发起请求,定义代理
    [NSURLConnection connectionWithRequest:request delegate:self];
    
}

// 分批返回数据
- (void)connection:(NSURLConnection *) connection didReceiveData:(NSData *)data {
    [_data appendData:data];
    NSLog(@"%@", _data);
}

// 数据完全返回完毕
- (void)connectionDidFinishLoading:(NSURLConnection *)connection {
    NSString *dataString =  [[NSString alloc] initWithData:_data encoding:NSUTF8StringEncoding];
    NSLog(@"%@", dataString);
}


4.iOS Http get 请求

// 组合一个搜索字符串
    NSString *urlStr = [NSString stringWithFormat:@"http://www.baidu.com/s?wd=%@", @"php"];
    // 字符串转化为URL
    NSURL *url = [NSURL URLWithString:urlStr];
    
//    NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];
//    [request setURL:url];
//    [request setHTTPMethod:@"POST"];
//    [request setTimeoutInterval:60];
//    [request setHTTPBody:_data];
//    [request setValue:@"ttt" forHTTPHeaderField:@"cookies"];
   
    // url转化为一个请求
    NSURLRequest *request = [NSURLRequest requestWithURL:url];
    // 状态请求
    NSURLResponse *response;
    // 链接一个请求
    NSData *resultData = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:nil];
    // 返回数据转为字符串
    NSData *dataString = [[NSString alloc] initWithData:resultData encoding:NSUTF8StringEncoding];
    NSLog(@"%@", dataString);
// 解析json吧


5.NSURL 基本方法

    NSURL *url = [NSURL URLWithString:@"http://www.baidu.com/search?id=1"];
    NSLog(@"scheme:%@", [url scheme]); //协议 http
    NSLog(@"host:%@", [url host]);     //域名 www.baidu.com
    NSLog(@"absoluteString:%@", [url absoluteString]); //完整的url字符串 http://www.baidu.com:8080/search?id=1
    NSLog(@"relativePath: %@", [url relativePath]); //相对路径 search
    NSLog(@"port :%@", [url port]);  // 端口 8080
    NSLog(@"path: %@", [url path]);  // 路径 search
    NSLog(@"pathComponents:%@", [url pathComponents]); // search
    NSLog(@"Query:%@", [url query]);  //参数 id=1


6.软件打开滑动导航 UIScrollView

- (void)viewDidLoad
{
    [super viewDidLoad];
	// Do any additional setup after loading the view.
    _scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, 340, 460)];
    _scrollView.backgroundColor = [UIColor whiteColor];
    _scrollView.delegate = self;
    _scrollView.pagingEnabled = YES;
    _scrollView.tag = INT_MAX;
    _scrollView.showsHorizontalScrollIndicator = NO;
    
    _scrollView.contentSize = CGSizeMake(340 * 4, 460);
    [self.view addSubview:_scrollView];
    
    int _x = 0;
    
    for (int index = 0; index < 4; index++) {
        UIImageView *imgScrollView = [[UIImageView alloc] initWithFrame:CGRectMake(0+_x, 0, 320, 460)];
        imgScrollView.tag = index;
        NSString *imgName = [NSString stringWithFormat:@"%d.JPG", index + 1];
        imgScrollView.image = [UIImage imageNamed:imgName];
        [_scrollView addSubview:imgScrollView];
        _x += 340;
        
    }
    
    UIPageControl *pageControl = [[UIPageControl alloc] initWithFrame:CGRectMake(0, 180, 320, 50)];
    pageControl.numberOfPages = 4;
    pageControl.tag = 101;
    [self.view addSubview:pageControl];
    [pageControl release];
}

- (void) scrollViewDidEndDecelerating:(UIScrollView *)scrollView {
    int current = scrollView.contentOffset.x / 320;
    UIPageControl *pageControl = (UIPageControl *)[self.view viewWithTag:101];
    pageControl.currentPage = current;
    
}

7.Objective-c 类初始化参数

#import <Foundation/Foundation.h>

@interface obj : NSObject
{
	NSString *_name;
}
-(id) initWithName:(NSString *) name;
-(void) setName:(NSString *)name;
-(NSString *) getName;

@end

@implementation obj

-(NSString *) getName {
	return _name;	
}

-(void)setName:(NSString *) name {
	_name = name;
}

 // 初始化方法,带参数
-(id) initWithName:(NSString *) name {
	// 调用父类init 生成类
	self = [super init];
	
	if (self) {
		// 执行自己的方法
		[self setName:name];
	}
	return self;
}
@end

int main(int argc, char *argv[]) {
	@autoreleasepool {
		obj *o = [[obj alloc] initWithName:@"wangdk"];
		NSLog(@"name is %@", [o getName]);
		
	}
}

8.Objective-c 处理动态类型的方法

#import <Foundation/Foundation.h>

//-(BOOL) isKindOfClass:class // 对象是不是class或其子类成员
//-(BOOL) isMemberOfClass:class // 对象是不是class的成员
//-(BOOL) respondsToSelector:selector // 对象是否能够相应selector所指定的方法
//+(BOOL) instancesRespondToSelector:selector // 指定对象实力是否能响应selector
//+(BOOL) isSubclassOfClass:class // 对象是否指定类的子类
//-(id) performSwlector:selector // 应用selector指定的方法
//-(id) perforumSelector:selector widthObject:object // 应用selector指定方法传参object

@interface obj : NSObject

@end

@implementation obj

@end

@interface obj2 : obj

-(void)setName;
@end

@implementation obj2
-(void)setName {
	
}
@end

int main(int argc, char *argv[]) {
	@autoreleasepool {
		obj *o = [[obj2 alloc] init];
		// 判断 o 是不是属于obj 实例或子类实例
		if ([o isKindOfClass: [obj class]] == YES) {
			NSLog(@" obj is a Kind of o class");
		}
		// 判断 o 是不是属于 obj 实例
		if ([o isMemberOfClass:[obj class]] == YES ) {
			NSLog(@"obj is member of class of o class");
		}
		
		// 判断o是否可以响应setName方法
		if ([o respondsToSelector:@selector(setName)] == YES) {
			NSLog(@" o respondsToSelector setName");
		}
		
	}
}


9.IOS获取当月天数

NSCalendar *calendar = [NSCalendar currentCalendar];
    
    unsigned unitFlags = NSYearCalendarUnit | NSMonthCalendarUnit |  NSDayCalendarUnit;
    
    NSDateComponents *components = [calendar components:unitFlags fromDate:[NSDate date]];
    
    NSInteger iCurYear = [components year];  //当前的年份
    
    NSInteger iCurMonth = [components month];  //当前的月份
    
    NSInteger iCurDay = [components day];  // 当前的号数
    
    NSString  *dateStr = nil;
    NSMutableArray *arr = [NSMutableArray array];
    
    for (NSInteger i = 1; i <= iCurDay; i++) {
        dateStr  = [NSString stringWithFormat:@"%d-%d-%d", iCurYear, iCurMonth, i];
        [arr addObject:dateStr];
    }
    
    _arrayList = [arr copy];


10.UITableDelgate 几种代理方法

#pragma mark - TableView delegate
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.row == 0 && indexPath.section == 2) {
        return 80; // 第三个section中第一行
    }return 44;
} // 设置行高

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    if (section == 0) {
        return 44;
    }return 25;
} // 设置section header的高度


- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    if (section == 12) {
        return 80;
    }return 50;
} // 设置section footer的高度

/*
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    UIView *headerView = [[UIView alloc] initWithFrame:CGRectZero];
    headerView.backgroundColor = [UIColor cyanColor];
    return [headerView autorelease];
}*/ // 设置section自定义头部视图


- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section
{
    UIView *footerView = [[UIView alloc] initWithFrame:CGRectZero];
    footerView.backgroundColor = [UIColor cyanColor];
    
    UILabel *tipLabel = [[UILabel alloc] initWithFrame:CGRectMake(60, 0, 200, 30)];
    tipLabel.numberOfLines = 0;
    tipLabel.textAlignment = NSTextAlignmentCenter;
    tipLabel.text = [NSString stringWithFormat:@"section footer %d", section+1];
    [footerView addSubview:tipLabel];
    [tipLabel release];
    
    return [footerView autorelease];
} // 设置section自定义尾部视图




你可能感兴趣的:(ios,框架,网络,Objective-C,摄像头)