ios 常用的工具

mindX發表於2019-03-25

生成二維碼

//生成的二維碼不是很清晰 需要呼叫下面的方法 生成清晰的二維碼
+ (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";
    }
}
複製程式碼

相關文章