In recent projects, image security needs to be processed to ensure that users (even jailbreaking users) cannot view the images downloaded from the server.
After a variety of methods, it is found that it is most convenient to use base64 to encode the stored files. After encoding, when the user opens the image, it will be displayed in full black.
Encoding method:
NSString * path1 = [NSHomeDirectory () stringByAppendingFormat: @ "/Documents/bronze/download/img_0_0_0.png"]; // convert the file path to data NSData * imageData0 = [NSData dataWithContentsOfFile: path1]; // base64 encoded NSData * imageData = [GTMBase64 encodeData: imageData0]; [imageData writeToFile: path1 atomically: YES];
Decoding display:
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];
Reference: http://blog.csdn.net/yshen_dublin/article/details/4416209