关闭

ios开发中常用的几种辅助方法

标签: iosios开发objective-c
386人阅读 评论(0) 收藏 举报
分类:
//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;
}

0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:40987次
    • 积分:1274
    • 等级:
    • 排名:千里之外
    • 原创:86篇
    • 转载:18篇
    • 译文:0篇
    • 评论:2条
    文章存档