常见的小知识
修改图片尺寸
UIImage * image = nil;
CGSize size = nil;
UIGraphicsBeginImageContext(size);
[image drawInRect:CGRectMake(0, 0, , size.width ,size.height)];
UIImage * changedImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
代码实现autoresizing
redView.autoresizingMask
CGRect CGRectInset ( CGRect rect, CGFloat dx, CGFloat dy );
以原来的rect为基础 改变宽和高 改变的幅度根据dx dy来决定
两个视图的中心相同 只有大小不同
CGRect CGRectOffset( CGRect rect, CGFloat dx, CGFloat dy );
以原来的rect的左上角原点为基础 沿X轴Y轴进行移动 移动量根据dx和dy而定
ARC与MRC混编 BuildPhases>>ComplieSource>> -fno-objc-arc
hidesBottomBarWhenPushed隐藏下方的tabbar
[UIApplication sharedApplication].keyWindow.rootViewController = mainViewController;设置主控制器
调用代理方法时判断是否响应
if ([self respondsToSelector:]){ }
获取图片的大小
NSStringFromCGSize(imge.size);
定义枚举类
/枚举 和宏定义相似
//(枚举的类型 枚举的名字)
//下载的过程中标题的切换
typedef NS_ENUM(NSInteger, ZYButtonState){
//正常状态
ZYButtonStateNomal =0,
//下载状态
ZYButtonStateDownloading,
//暂停状态
ZYButtonStatePause,
//完成状态
ZYButtonStateComletion,
};
字符串NSString
NSString*string =@"sdfsfsfsAdfsdf";
string = [string substringToIndex:7];//截取掉下标7之后的字符串
NSLog(@"截取的值为:%@",string);
[string substringFromIndex:2];//截取掉下标2之前的字符串
NSLog(@"截取的值为:%@",string);
2.匹配字符串
NSString*string =@"sdfsfsfsAdfsdf";
NSRangerange = [stringrangeOfString:@"f"];//匹配得到的下标
NSLog(@"rang:%@",NSStringFromRange(range));
string = [string substringWithRange:range];//截取范围类的字符串
NSLog(@"截取的值为:%@",string);
3.分隔字符串
NSString*string =@"sdfsfsfsAdfsdf";
NSArray *array = [string componentsSeparatedByString:@"A"]; //从字符A中分隔成2个元素的数组
NSLog(@"array:%@",array); //结果是adfsfsfs和dfsdf
[字符串对象 stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]]; 去空格和换行
字符串分割
NSString *abc =@"1|2|3|4|5”;
NSArray *arr =[abc componentsSeparatedByString:@"|”]
替换 把a替换为00
[self stringByReplacingOccurrencesOfString:@“a" withString:@"00"];
NSString *urlString =[NSString stringWithFormat:@"http://apis.baidu.com/apistore/weatherservice/citylist?cityname=郑州&apikey=614ea11c21b05ee11ca45e2e1f44a711”];
字符串编码
NSString *newString =[urlString stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
两个数组的第一个元素是否相同,如果相同,则返回 数组中,第一个元素的字符串,反之,返回null 对象
- (id)firstObjectCommonWithArray:(NSArray *)otherArray;
* 计算文字尺寸
*
* @param text 需要计算尺寸的文字
* @param font 文字的字体
* @param maxSize 文字的最大尺寸
*/
- (CGSize)sizeWithText:(NSString *)text font:(UIFont *)font maxSize:(CGSize)maxSize
{
NSDictionary *attrs = @{NSFontAttributeName : font};
return [text boundingRectWithSize:maxSize options:NSStringDrawingUsesLineFragmentOrigin attributes:attrs context:nil].size;
}
导航栏上的右键
UIBarButtonItem *rightItem =[[UIBarButtonItem alloc]initWithTitle:@"上传头像" style:UIBarButtonItemStylePlain target:self action:@selector(buttonAction)]; self.navigationItem.rightBarButtonItem =rightItem;
segmentedControl = [[UISegmentedControl alloc] initWithFrame:CGRectMake(100, 100, 200, 30)];
[segmentedControl insertSegmentWithTitle:@"公共资源" atIndex:0 animated:YES];
[segmentedControl insertSegmentWithTitle:@"个人资源" atIndex:1 animated:YES];
segmentedControl.selectedSegmentIndex = 0;
[segmentedControl addTarget:self action:@selector(switchOver:) forControlEvents:UIControlEventValueChanged];
self.navigationItem.titleView = segmentedControl;
UIButton * localFileButton = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 30, 30)];
[localFileButton setImage:[UIImage imageNamed:@"download_button"] forState:UIControlStateNormal];
[localFileButton addTarget:self action:@selector(localFileButtonClick) forControlEvents:UIControlEventTouchUpInside];
UIBarButtonItem *rightItem =[[UIBarButtonItem alloc]initWithCustomView:localFileButton];
self.navigationItem.rightBarButtonItem =rightItem;
创建图片选择器
UIImagePickerController * imgPicker = [[UIImagePickerController alloc] init];
imgPicker.delegate = self;
imgPicker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
imgPicker.allowsEditing = YES;
[self presentViewController:imgPicker animated:YES completion:nil];
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingImage:(UIImage *)image editingInfo:(NSDictionary *)editingInfo NS_DEPRECATED_IOS(2_0, 3_0)
{选中的图片
NSMutableData * data = (NSMutableData *)UIImagePNGRepresentation(image);
[self dismissViewControllerAnimated:YES completion:nil];
}
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{}
//取消
- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker
{
[self dismissViewControllerAnimated:YES completion:nil];
}
设置scrollview的默认位置
[_scrollView setContentOffset:CGPointMake(320*segmentedControl.selectedSegmentIndex,0) animated:YES];
写文件
NSFileManager *fm =[NSFileManager defaultManager];
NSString *directoryPath =[NSHomeDirectory() stringByAppendingPathComponent:@"tmp/Downloads"];
if (![fm fileExistsAtPath:directoryPath])
{
[fm createDirectoryAtPath:directoryPath withIntermediateDirectories:YES attributes:nil error:nil];下载时候有文件夹就够了 下载的时候会有加路径的代码
}
NSString *filePath =[NSHomeDirectory() stringByAppendingPathComponent:[NSString stringWithFormat:@"tmp/Downloads/%@",name]];
if (![fm fileExistsAtPath:filePath])
{
[fm createFileAtPath:filePath contents:nil attributes:nil];文件写入本地需要创建到文件名
}
清理NSUserDefaults
NSUserDefaults * user = [NSUserDefaults standardUserDefaults];
[user removeObjectForKey:@"token"];
[user removeObjectForKey:@"time"];
xib文件中有多个view时拖线连属性需要注意属性关联的对象
添加block块属性@property (nonatomic,copy) void(^block)(void);
调用块 self.block(); 不谢void
ASI开启断点下载
配置asi
1.SystemConfiguration
2.CFNetwork
3.libz.1.2.5.dylib
4.MobileCoreServices
-(void)downLoadWithString:(NSString *)string name:(NSString *)name
{
NSLog(@"下载开始");
aRequest =[[ASIHTTPRequest alloc]initWithURL:[NSURL URLWithString:string]];
aRequest.downloadProgressDelegate =self;
NSString *documentsPath =[self getFilePathWithFileName:name];
aRequest.downloadDestinationPath =documentsPath;
NSString *temp =[self getTmpFilePathWithFileName:name];
aRequest.temporaryFileDownloadPath=temp;
aRequest.allowResumeForFileDownloads =YES;
aRequest.delegate =self;
[aRequest startAsynchronous];
}
- (void)requestFinished:(ASIHTTPRequest *)request
{
NSLog(@"下载结束===");
self.finishBlock();
}
- (void)setProgress:(float)newProgress
{
NSLog(@"当前的进度---%f",newProgress);
self.block(newProgress);
}
数组字典字符串数据源 之所以可以写入本地,是因为他们都实现了NSCoding协议
NSCoding协议规定了一套编码和解码的规范,只有实现NSCoding协议的对象才可以直接写入本地
base64EncodedString编码
base64DecodedString解码
拉伸图片
LeftCap左边固定像素 topCap上固定像素 固定了图片左上角进行拉伸
UIImage * laShenImg = [img stretchableImageWithLeftCapWidth:23 topCapHeight:14];
越狱前后功能
沙盒前往 》 资源库 》Application Support 》 IPhone Simulator
iOS Device设备 真机测试
ARC需要关彻底
iOS手机端 不一样
越狱之后才能根据SD卡查看
UISwitch点击后先改变自己的值再执行方法
NSArray * paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
时间NSDate
判断时间
if ([date compare:expires]==NSOrderedDescending)
{
return NO;
}
a(newDate) b(expriseDate)
NSOrderedAscending a<b
NSOrderedSame, a=b
NSOrderedDescending a>b
时间格式
//先用第一个工具类初始化一个正常的date
NSDateFormatter *iosForM =[[[NSDateFormatter alloc]init]autorelease];
//设置成新浪返回的格式
iosForM.dateFormat =@"EEE MMM dd HH:mm:ss Z yyyy";
//如果不配置当前date的时区,是转换不成功的
iosForM.locale =[[NSLocale alloc]initWithLocaleIdentifier:@"en_US"];
//正常时间
NSDate *date =[iosForM dateFromString:aString];
//MM月dd日
NSDateFormatter *fm =[[NSDateFormatter alloc]init];
fm.dateFormat =@"MM月dd日 HH:mm:ss";
return [fm stringFromDate:date];
浙公网安备 33010602011771号