Skip to content

Instantly share code, notes, and snippets.

@yxjxx
Last active August 29, 2015 14:21
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save yxjxx/1f0896e51eea03f49458 to your computer and use it in GitHub Desktop.
Save yxjxx/1f0896e51eea03f49458 to your computer and use it in GitHub Desktop.
NSString *bundlePath = [[NSBundle mainBundle] bundlePath];
NSLog(@"bundlePath is %@",bundlePath);
NSString *documentDirectory = [NSSearchPathForDirectoriesInDomains(NSDocumentationDirectory, NSUserDomainMask, YES) lastObject];
NSLog(@"documentDirectory is %@", documentDirectory);
NSString *libraryDirectory = [NSSearchPathForDirectoriesInDomains(NSLibraryDirectory, NSUserDomainMask, YES) lastObject];
NSLog(@"libraryDirectory is %@", libraryDirectory);
NSString *cacheDirectory = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) lastObject];
NSLog(@"cacheDirectory is %@", cacheDirectory);
NSFileManager *fileManager = [NSFileManager defaultManager];
NSString *filePathName = [documentDirectory stringByAppendingPathComponent:@"1.txt"];
NSError *error = nil;
NSDictionary *fileAttributes = [fileManager attributesOfItemAtPath:filePathName
error:&error];
if (!error) {
NSDate *dateFileCreated = [fileAttributes valueForKey:NSFileCreationDate];
NSString *fileType = [fileAttributes valueForKey:NSFileType];
NSLog(@"This %@ file was created on %@", fileType, dateFileCreated);
}
NSArray *listOfFiles = [fileManager contentsOfDirectoryAtPath:documentDirectory error:&error];
if (!error) {
NSLog(@"Contents of Documentation directory: %@",listOfFiles);
}
NSArray *listOfSubPaths = [fileManager subpathsOfDirectoryAtPath:documentDirectory error:&error];
if (!error) {
NSLog(@"Sub paths of Documentation directory: %@",listOfSubPaths);
}
//
// ViewController.m
// FileEncrypt
//
// Created by yxj on 5/20/15.
// Copyright (c) 2015 yxj. All rights reserved.
//
#import "ViewController.h"
@interface ViewController ()
@property (weak, nonatomic) IBOutlet UIImageView *imageView;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
NSString *documentDirectory = [NSSearchPathForDirectoriesInDomains(NSDocumentationDirectory, NSUserDomainMask, YES) lastObject];
NSLog(@"%@",documentDirectory);
NSString *filePathName = [documentDirectory stringByAppendingPathComponent:@"1.jpg"];
//把待上传的文件读入NSData
NSData *fileData = [[NSData alloc] initWithContentsOfFile:filePathName];
[self.imageView setImage:[UIImage imageWithData:fileData]];
NSMutableData *afterFile = [[NSMutableData alloc] init];
[afterFile appendData:fileData];
NSMutableData *keyContainer = [[NSMutableData alloc] init];
NSLog(@"file: %@",fileData);
//跳过文件头,均匀的抽取,存到一个字符串中,作为key
//key的结构是offset占4个字节,length占4个字节,content占自己的大小,因为我采用均匀抽取所以每一段的大小都是固定的。
// [keyContainer appendData:[NSData dataWithBytes:&offset length:sizeof(offset)]];
// NSLog(@"%@",keyContainer);
int PERCENTAGE = 1;
int ex_location = 0;
int ex_length = 0;
ex_length = PERCENTAGE;
int fileHeaderLength = 16;
for(ex_location += fileHeaderLength; ex_location+ex_length < fileData.length; ex_location += 100-ex_length){
[keyContainer appendData:[NSData dataWithBytes:&ex_location length:sizeof(ex_location)]];
[keyContainer appendData:[NSData dataWithBytes:&ex_length length:sizeof(ex_length)]];
[keyContainer appendData:[fileData subdataWithRange:NSMakeRange(ex_location, ex_length)]];
[afterFile resetBytesInRange:NSMakeRange(ex_location, ex_length)];
}
// NSLog(@"key: %@",keyContainer);
NSLog(@"file: %@",afterFile);
[self.imageView setImage:[UIImage imageWithData:afterFile]];
// [afterFile writeToFile:filePathName atomically:YES];
//把抽取了的位置填0
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment