1、self在block里强引用怎么处理?
首先,在block外面声明一个弱引用对象
__weak ViewController*weakSelf =self;
然后在block里,需要用到的self的地方用weakSelf代替即可。
2、将View转成Image
- (UIImage*)viewConvertImage:(UIView*)launchView{
CGSize imageSize = launchView.bounds.size;
UIGraphicsBeginImageContextWithOptions(imageSize,NO,[UIScreenmainScreen].scale);
[launchView.layerrenderInContext:UIGraphicsGetCurrentContext()];
UIImage*launchImage =UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return launchImage;
}
3、base64解码
- (UIImage*) dataURL2Image: (NSString*) imgSrc
{
NSData*data = [[NSData alloc] initWithBase64EncodedString:imgSrc options:0];
UIImage*image = [UIImage imageWithData: data];
return image;
}
4、清除数组中空的字段
- (NSMutableArray*)isNULLOrNotFromArray:(NSArray*)array
{
NSMutableArray*mutableArr = [NSMutableArray arrayWithCapacity:10];
for(NSDictionary*temp in array) {
NSMutableDictionary*dictionary = [NSMutableDictionary dictionaryWithDictionary:temp];
for(NSString*key in dictionary) {
id target = [dictionary objectForKey:key];
if([target isKindOfClass:[NSNull class]]) {
[dictionary removeObjectForKey:key];
}
}
[mutableArr addObject:dictionary];
}
return mutableArr;
}
5、获取当前时间
- (NSString*)GetCurrentTime
{
NSDate*date = [NSDate date];
NSDateFormatter*dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"YYYY/MM/dd"];
NSString*timeStr = [dateFormatter stringFromDate:date];
returntime Str;
}
6、清除缓存
-(void)ClearCache
{
CGFloatsize = [self folderSizeAtPath:NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask,YES).lastObject] + [self folderSizeAtPath:NSSearchPathForDirectoriesInDomains(NSLibraryDirectory,NSUserDomainMask,YES).lastObject] + [self folderSizeAtPath:NSTemporaryDirectory()];
NSString*message = size >1? [NSString stringWithFormat:@"缓存%.1fM,删除缓存", size] : [NSString stringWithFormat:@"缓存%.1fK,删除缓存", size *1024.0];
UIAlertController*alert = [UIAlertController alertControllerWithTitle:nil message:message preferredStyle:(UIAlertControllerStyleAlert)];
UIAlertAction*action = [UIAlertActionactionWithTitle:@"确定" style:(UIAlertActionStyleDefault) handler:^(UIAlertAction*action) {
[self cleanCaches:NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask,YES).lastObject];
[self cleanCaches:NSSearchPathForDirectoriesInDomains(NSLibraryDirectory,NSUserDomainMask,YES).lastObject];
[self cleanCaches:NSTemporaryDirectory()];
}];
//UIAlertAction *cancel = [UIAlertAction actionWithTitle:@"取消" style:(UIAlertActionStyleCancel) handler:nil];
UIAlertAction*cancel = [UIAlertAction actionWithTitle:@"取消" style:(UIAlertActionStyleCancel) handler:^(UIAlertAction*_Nonnullaction) {
}];
[alert addAction:action];
[alert addAction:cancel];
[self showDetailViewController:alert sender:nil];
}
//计算目录大小
- (CGFloat)folderSizeAtPath:(NSString*)path{
//利用NSFileManager实现对文件的管理
NSFileManager*manager = [NSFileManager defaultManager];
CGFloat size =0;
if([manager fileExistsAtPath:path]) {
//获取该目录下的文件,计算其大小
NSArray*childrenFile = [manager subpathsAtPath:path];
for(NSString*fileName in childrenFile) {
NSString*absolutePath = [path stringByAppendingPathComponent:fileName];
size += [manager attributesOfItemAtPath:absolutePath error:nil].fileSize;
}
//将大小转化为M
return size /1024.0/1024.0;
}
return 0;
}
//根据路径删除文件
- (void)cleanCaches:(NSString*)path{
//利用NSFileManager实现对文件的管理
NSFileManager*fileManager = [NSFileManager defaultManager];
if([fileManager fileExistsAtPath:path]) {
//获取该路径下面的文件名
NSArray*childrenFiles = [fileManager subpathsAtPath:path];
for(NSString*fileName in childrenFiles) {
//拼接路径
NSString*absolutePath = [path stringByAppendingPathComponent:fileName];
//将文件删除
[fileManager removeItemAtPath:absolutePath error:nil];
}
}
}
7、给键盘添加一个隐藏按钮
- (UIView*)getInputAccessoryView {
UIView*inputView = [[UIView alloc] initWithFrame:CGRectMake(0,0,self.view.frame.size.width,40)];
inputView.backgroundColor= [UIColor colorWithWhite:0.8 alpha:1.0];
UIButton*btn = [UIButton buttonWithType:UIButtonTypeCustom];
btn.frame=CGRectMake(inputView.frame.size.width-50,0,50, inputView.frame.size.height);
[btnsetTitle:@"隐藏" forState:UIControlStateNormal];
[btn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
btn.titleLabel.font= [UIFont systemFontOfSize:16];
[btn addTarget:self action:@selector(onHideKeyboard) forControlEvents:UIControlEventTouchUpInside];
[inputView addSubview:btn];
return inputView;
}
- (void)onHideKeyboard {
[self.view endEditing:NO];
[[UIApplication sharedApplication].keyWindow endEditing:NO];
}
8、