ios 常用的工具

生成二维码

//生成的二维码不是很清晰 需要调用下面的方法 生成清晰的二维码
+ (CIImage *)GenerateTheQRCode:(NSString *)url {
    //创建过滤器
    CIFilter *filter = [CIFilter filterWithName:@"CIQRCodeGenerator"];
    
    //过滤器恢复默认
    [filter setDefaults];
    
    //给过滤器添加数据
    NSString *string = url;
    
    //将NSString格式转化成NSData格式
    NSData *data = [string dataUsingEncoding:NSUTF8StringEncoding allowLossyConversion:YES];
    
    [filter setValue:data forKeyPath:@"inputMessage"];
    
    //获取二维码过滤器生成的二维码
    CIImage *image = [filter outputImage];
    return image;
}
+ (UIImage *)createNonInterpolatedUIImageFormCIImage:(CIImage *)image withSize:(CGFloat) size {
    CGRect extent = CGRectIntegral(image.extent);
    CGFloat scale = MIN(size/CGRectGetWidth(extent), size/CGRectGetHeight(extent));
    
    // 1.创建bitmap;
    size_t width = CGRectGetWidth(extent) * scale;
    size_t height = CGRectGetHeight(extent) * scale;
    CGColorSpaceRef cs = CGColorSpaceCreateDeviceGray();
    CGContextRef bitmapRef = CGBitmapContextCreate(nil, width, height, 8, 0, cs, (CGBitmapInfo)kCGImageAlphaNone);
    CIContext *context = [CIContext contextWithOptions:nil];
    CGImageRef bitmapImage = [context createCGImage:image fromRect:extent];
    CGContextSetInterpolationQuality(bitmapRef, kCGInterpolationNone);
    CGContextScaleCTM(bitmapRef, scale, scale);
    CGContextDrawImage(bitmapRef, extent, bitmapImage);
    
    // 2.保存bitmap到图片
    CGImageRef scaledImage = CGBitmapContextCreateImage(bitmapRef);
    CGContextRelease(bitmapRef);
    CGImageRelease(bitmapImage);
    return [UIImage imageWithCGImage:scaledImage];
}
复制代码

将HTML标签 转换成富文本

+ (NSMutableAttributedString *)setHtmlAttributedStingWithStr:(NSString *)str WithFontSize:(UIFont *)font{
    
    NSMutableAttributedString * rgStr = [[NSMutableAttributedString alloc] initWithData:[str dataUsingEncoding:NSUnicodeStringEncoding] options:@{NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType} documentAttributes:nil error:nil];
    
    [rgStr addAttributes:@{ NSFontAttributeName:font} range:NSMakeRange(0, rgStr.length)];
    return rgStr;
}
复制代码

生成随机单个字母或者数字

+(NSString *)return16LetterAndNumber{
    //定义一个包含数字,大小写字母的字符串
    NSString * strAll = @"0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
    //定义一个结果
    NSString * result = [[NSMutableString alloc]initWithCapacity:1];
    for (int i = 0; i < 1; i++)
    {
        //获取随机数
        NSInteger index = arc4random() % (strAll.length-1);
        char tempStr = [strAll characterAtIndex:index];
        result = (NSMutableString *)[result stringByAppendingString:[NSString stringWithFormat:@"%c",tempStr]];
    }
    
    return result;
}
复制代码

数组无序化

#pragma mark - 数组无序化
- (NSArray *)disorderlyArr:(NSArray *)arr {
    arr = [arr sortedArrayUsingComparator:^NSComparisonResult(NSString *str1, NSString *str2) {
        int seed = arc4random_uniform(2);
        if (seed) {
            return [str1 compare:str2];
        } else {
            return [str2 compare:str1];
        }
    }];
    return arr;
}
复制代码

获取当前时间是周几

#pragma mark - 获取当前周几的时间
- (NSString*)getCurrentWeekDay{
    NSTimeInterval interval = [[NSDate date] timeIntervalSince1970];
    return [self getWeekDayFordate:interval];
}
#pragma mark - 获取周几
- (NSString *)getWeekDayFordate:(NSTimeInterval)data {
NSArray *weekday = [NSArray arrayWithObjects: [NSNull null], @"周日", @"周一", @"周二", @"周三", @"周四", @"周五", @"周六", nil];
    
    NSDate *networkDate = [NSDate dateWithTimeIntervalSince1970:data];
    NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSCalendarIdentifierGregorian];
    NSDateComponents *components = [calendar components:NSCalendarUnitWeekday fromDate:networkDate];
    
    NSString *weekStr = [NSString stringWithFormat:@"星期%ld",components.weekday-1];
    return weekStr;
}
复制代码

app 启动是不是xcode 调起


- (NSString *)AmIBeingDebugged {
    int                 junk;
    int                 mib[4];
    struct kinfo_proc   info;
    size_t              size;
    
    // Initialize the flags so that, if sysctl fails for some bizarre
    // reason, we get a predictable result.
    
    info.kp_proc.p_flag = 0;
    
    // Initialize mib, which tells sysctl the info we want, in this case
    // we're looking for information about a specific process ID.
    
    mib[0] = CTL_KERN;
    mib[1] = KERN_PROC;
    mib[2] = KERN_PROC_PID;
    mib[3] = getpid();
    
    // Call sysctl.
    
    size = sizeof(info);
    junk = sysctl(mib, sizeof(mib) / sizeof(*mib), &info, &size, NULL, 0);
    assert(junk == 0);
    
    // We're being debugged if the P_TRACED flag is set.
    bool ? = ( (info.kp_proc.p_flag & P_TRACED) != 0 );
    NSMutableString  *tempzStr = [[NSMutableString alloc] initWithString:@"2"];
    if (?) {
        [tempzStr appendString:[self randumNumStr]];
    } else {
        [tempzStr appendString:@"0"];
    }
    
    return tempzStr;
}

复制代码

app 是不是运行在模拟器

- (BOOL)runPhoneOrSimulator {
    //如果是模拟器
    if (TARGET_IPHONE_SIMULATOR) {
        return YES;
    } else {
        return NO;
    }
}
复制代码

手机是否开启代理

#pragma mark - 是否开启代理
- (NSString *)fetchHttpProxy {
    CFDictionaryRef dicRef = CFNetworkCopySystemProxySettings();
    const CFStringRef proxyCFstr = (const CFStringRef)CFDictionaryGetValue(dicRef,
                                                                           (const void*)kCFNetworkProxiesHTTPProxy);
    NSString* proxy = (__bridge NSString *)proxyCFstr;
    if (proxy.length>0) {
        return @"h0";
    }else {
        return @"h1";
    }
}
复制代码

转载于:https://juejin.im/post/5c9885f7f265da612009973c

你可能感兴趣的:(ios 常用的工具)