ios開發中常用的幾種輔助方法
阿新 • • 發佈:2019-02-18
//1.Keychain本地長期鍵值儲存 //刪除 +(void)deleteStringForKey:(NSString *)aKey { NSMutableDictionary *query = [NSMutableDictionary dictionary]; [query setObject:(__bridge id)kSecClassGenericPassword forKey:(__bridge id)kSecClass]; [query setObject:(id)aKey forKey:(__bridge id)kSecAttrAccount]; OSStatus status = SecItemDelete((__bridge CFDictionaryRef)query); if (status != noErr) { // NSLog(@"[KeychainAccessor]>>> SecItemDelete result in error:(%d)", (int)status); } } //儲存 + (void)setString:(NSString *)aString forKey:(NSString *)aKey { NSData *savingData = [aString dataUsingEncoding:NSUTF8StringEncoding]; NSMutableDictionary *attributes = [NSMutableDictionary dictionary]; [attributes setObject:(__bridge id)kSecClassGenericPassword forKey:(__bridge id)kSecClass]; [attributes setObject:(id)aKey forKey:(__bridge id)kSecAttrAccount]; [attributes setObject:savingData forKey:(__bridge id)kSecValueData]; OSStatus status = SecItemAdd((__bridge CFDictionaryRef)attributes, NULL); if ((int)status==-25299) { // NSLog(@"delete old data add new data"); [self deleteStringForKey:aKey]; SecItemAdd((__bridge CFDictionaryRef)attributes, NULL); } if (status != noErr) { // NSLog(@"[KeychainAccessor]>>> SecItemAdd result in error:(%d)",(int)status); } } //查詢 + (NSString *)stringForKey:(NSString *)aKey { NSMutableDictionary *query = [NSMutableDictionary dictionary]; [query setObject:(__bridge id)kSecClassGenericPassword forKey:(__bridge id)kSecClass]; [query setObject:(id)aKey forKey:(__bridge id)kSecAttrAccount]; [query setObject:(__bridge id)kCFBooleanTrue forKey:(__bridge id)kSecReturnData]; [query setObject:(__bridge id)kSecMatchLimitOne forKey:(__bridge id)kSecMatchLimit]; CFDataRef result = nil; OSStatus status = SecItemCopyMatching((__bridge CFDictionaryRef)query, (CFTypeRef*)&result); if (status != noErr) { if (status == errSecItemNotFound) { //NSLog(@"[KeychainAccessor]>>> SecItemCopyMatching result NOT-FOUND."); } else { //NSLog(@"[KeychainAccessor]>>> SecItemCopyMatching result in error:(%d)", (int)status); } return @""; } NSData *theValue = [(__bridge NSData*)result copy]; return [[NSString alloc] initWithData:theValue encoding:NSUTF8StringEncoding]; } //2.壓縮圖片到指定尺寸大小 + (UIImage *)compressOriginalImage:(UIImage *)image toSize:(CGSize)size{ UIImage *resultImage = image; UIGraphicsBeginImageContext(size); [resultImage drawInRect:CGRectMake(0, 0, size.width, size.height)]; UIGraphicsEndImageContext(); return resultImage; } //3.壓縮圖片到指定檔案大小 + (NSData *)compressOriginalImage:(UIImage *)image toMaxDataSizeKBytes:(CGFloat)size{ NSData *data = UIImageJPEGRepresentation(image, 1.0); CGFloat dataKBytes = data.length/1000.0; CGFloat maxQuality = 0.9f; CGFloat lastData = dataKBytes; while (dataKBytes > size && maxQuality > 0.01f) { maxQuality = maxQuality - 0.01f; data = UIImageJPEGRepresentation(image, maxQuality); dataKBytes = data.length/1000.0; if (lastData == dataKBytes) { break; }else{ lastData = dataKBytes; } } return data; } //全屏截圖 + (UIImage *)shotScreen{ UIWindow *window = [UIApplication sharedApplication].keyWindow; UIGraphicsBeginImageContext(window.bounds.size); [window.layer renderInContext:UIGraphicsGetCurrentContext()]; UIImage *image = UIGraphicsGetImageFromCurrentImageContext(); UIGraphicsEndImageContext(); return image; }