一種簡便的ios圖片加密方法-對圖片進行base64編碼


最近項目中需要對圖片的安全做處理,保證用戶(即使是越獄用戶)也不能查看到從服務器中下載下來的圖片。

折騰了多種方法,發現還是使用base64對儲存的文件進行一次編碼最方便,編碼之后,用戶打開圖片時將顯示為全黑色

編碼方法:

NSString *path1 = [NSHomeDirectory() stringByAppendingFormat:@"/Documents/bronze/download/img_0_0_0.png"];
            //文件路徑轉換為data
            NSData *imageData0 = [NSData dataWithContentsOfFile:path1];
            //對data進行base64編碼
            NSData *imageData = [GTMBase64 encodeData:imageData0];
            
            [imageData writeToFile:path1 atomically:YES];

解碼顯示:

    NSString *imagePath = [NSHomeDirectory() stringByAppendingFormat:@"/Documents/bronze/download/img_0_0_0.png"];
    NSData *data = [NSData dataWithContentsOfFile:imagePath];
    NSData *data1 = [GTMBase64 decodeData:data];
    
    UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(100, 20, 300, 400)];
    imageView.image = [UIImage imageWithData:data1];
    imageView.backgroundColor = [UIColor redColor];
    [self.view addSubview:imageView];

 

 

 

 

參考:http://blog.csdn.net/yshen_dublin/article/details/4416209


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM