1.将图片保存到目录里面
创建对应的文件夹
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *path = [paths objectAtIndex:0];
NSFileManager *fileManager = [NSFileManager defaultManager];
NSString *file = [NSString stringWithFormat:@"%@",self.modelHome.customerName];
NSString *file1 = [NSString stringWithFormat:@"%@",self.lageModel.nameStage];
NSString *file2 = [NSString stringWithFormat:@"%@",self.attName.text];
NSString *iOSDirectory1 = [path stringByAppendingPathComponent:file];
NSString *iOSDirectory2 = [iOSDirectory1 stringByAppendingPathComponent:file1];
NSString *iOSDirectory3 = [iOSDirectory2 stringByAppendingPathComponent:file2];
BOOL isSuccess = [fileManager createDirectoryAtPath:iOSDirectory3 withIntermediateDirectories:YES attributes:nil error:nil];// 创建对应的文件夹
if (isSuccess) {
NSLog(@"success");
} else {
NSLog(@"fail");
}
修改照片的大小尺寸,
UIGraphicsBeginImageContext(CGSizeMake(self.image.size.width*0.9, self.image.size.height*0.9));
[self.image drawInRect:CGRectMake(0,0,self.image.size.width*0.9,self.image.size.height*0.9)];
UIImage* newImage = UIGraphicsGetImageFromCurrentImageContext();
NSLog(@"newImage%@",newImage);
UIGraphicsEndImageContext();
//~>下面代码通过循环将图片稳定在400k以内
NSData *data1 = UIImageJPEGRepresentation(self.image ,1);
self.data = data1;
long long alength= self.data.length;
NSInteger i = 0;
for (NSInteger j = 0 ; j < 10; j ++) {
if (alength/1024 > 300) {
i ++;
NSData *data1 = UIImageJPEGRepresentation(self.image ,1-i * 0.1);
self.data = data1;
alength= self.data.length;
NSLog(@"alength%lldk",alength/1024);
}else{
break;
}
}
alength= self.data.length;
将图片保存到目录里面 _count 这个是静态变量,给图片设置对应的id ID唯一对应每张图片,方便取出对应的每一张照片
self.fullPath1 = [path stringByAppendingFormat:@"/%@/%@/%@/%@_%ld.jpg",file,file1,file2,self.attName.text,_count];
NSLog(@"self.fullpath1%@",self.fullPath1);
[fileManager createFileAtPath:self.fullPath1 contents:self.data attributes:nil];
2.获取目录内部的照片并且展示出来
#pragma mark - 进来加载目录图片
-(void)refreshImage{
清空展示试图
[self.tempNextArray removeAllObjects];
for (UIView * subview in [self.scrollView subviews]) {
[subview removeFromSuperview];
}
[self.numDataSource removeAllObjects];
获取目录
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSFileManager *fileManager = [NSFileManager defaultManager];
NSString *file = [NSString stringWithFormat:@"%@",self.modelHome.customerName];
NSString *file1 = [NSString stringWithFormat:@"%@",self.lageModel.nameStage];
NSString *file2 = [NSString stringWithFormat:@"%@",self.attName.text];
self.myString = [NSString stringWithFormat:@"/%@/%@/%@",file,file1,file2];
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *myDirectory = [documentsDirectory stringByAppendingPathComponent:self.myString];
NSArray *files = [fileManager subpathsOfDirectoryAtPath: myDirectory error:nil];
self.yixuanzhaopian.text = [NSString stringWithFormat:@"已选中照片:%ld张",files.count];
self.yixuanzhaopian.font = [UIFont systemFontOfSize:15];
self.scrollView.contentSize = CGSizeMake((self.scrollView.height*WIDTH/HEIGHT +5) * files.count, 0);
NSInteger num=0;
取出目录中的id放入数组,id其实就是一个数字将这些数字放入到数组里面
for (NSString *str in files) {
NSScanner *scanner = [NSScanner scannerWithString:str];
[scanner scanUpToCharactersFromSet:[NSCharacterSet decimalDigitCharacterSet] intoString:nil];
int number;
[scanner scanInt:&number];
[self.numDataSource addObject:@(number)];
}
对取出来的数字id数组进行排序
NSArray *sortedArray = [self.numDataSource sortedArrayUsingComparator:^NSComparisonResult(id obj1, id obj2){
if (obj1 > obj2){
return NSOrderedDescending;
}
if (obj1 < obj2 ){
return NSOrderedAscending;
}
return NSOrderedSame;
}];
NSLog(@"排序后的数组:%@",sortedArray);
if (sortedArray.count == 0) {
[self.countArray removeAllObjects];
}
[self.countArray addObjectsFromArray:sortedArray];
通过排序好的数组进行取出对应生序的目录文件夹数组
for (NSInteger i = 0 ; i < files.count; i ++) {
NSString *string = [NSString stringWithFormat:@"%@.jpg",sortedArray[i]];
NSLog(@"string%@",string);
num++;
for (NSString *str in files) {
NSRange range = [str rangeOfString:string];
if(range.location != NSNotFound)
{
NSString *str1 = [NSString stringWithFormat:@"%@/%@",myDirectory,str];
将拼接好的目录取出对应的图片
UIImage *img = [UIImage imageWithContentsOfFile:str1];
[self.tempNextArray addObject:str];
创建View展示image
UIView * myview = [[UIView alloc]initWithFrame:CGRectMake((self.scrollView.height*WIDTH/HEIGHT+5)*(num-1)+5, 0, self.scrollView.height*WIDTH/HEIGHT, self.scrollView.height)];
// myview.backgroundColor = [UIColor redColor];
myview.layer.cornerRadius = 8;
myview.layer.borderWidth = 1;
myview.layer.borderColor = [[UIColor redColor] CGColor];
[self.scrollView addSubview:myview];
self.imageView1 = [[CustomView alloc]initWithFrame:CGRectMake(0, 20, self.scrollView.height*WIDTH/HEIGHT, self.scrollView.height-40)];
self.upLabel = [[UILabel alloc]initWithFrame:CGRectMake(0, CGRectGetMaxY(self.imageView1.frame), self.scrollView.height*WIDTH/HEIGHT, 20)];
NSRange range = [str rangeOfString:@"_"];
self.upLabel.text = [NSString stringWithFormat:@"%@%ld",[str substringToIndex:range.location],i+1];
self.upLabel.textAlignment = NSTextAlignmentCenter;
self.upLabel.font = [UIFont systemFontOfSize:10];
[myview addSubview:self.upLabel];
self.serialnumberLabel = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, self.scrollView.height*WIDTH/HEIGHT, 20)];
NSScanner *scanner1 = [NSScanner scannerWithString:str];
[scanner1 scanUpToCharactersFromSet:[NSCharacterSet decimalDigitCharacterSet] intoString:nil];
int number1;
[scanner1 scanInt:&number1];
self.serialnumberLabel.text = [NSString stringWithFormat:@"序号:%d",number1];
self.serialnumberLabel.textAlignment = NSTextAlignmentCenter;
self.serialnumberLabel.font = [UIFont systemFontOfSize:10];
[myview addSubview:self.serialnumberLabel];
self.imageView1.image = img;
imageView的tag值设置为每张图片对应的数字id
self.imageView1.tag = [sortedArray[i] integerValue];
添加手势
UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapGesture:)];
tapGesture.numberOfTapsRequired = 1; //点击次数
tapGesture.numberOfTouchesRequired = 1; //点击手指数
self.imageView1 .userInteractionEnabled = YES;
[self.imageView1 addGestureRecognizer:tapGesture];
[myview addSubview: self.imageView1];
}
}
}
}
3.在最前面位置插入一张图片
-(void)insetPast{
PhotoViewController *pvc1 = [[PhotoViewController alloc]init];
if (self.removeIndex != [self.numDataSource[0] integerValue]) {
UIAlertView *al = [[UIAlertView alloc]initWithTitle:@"温馨提示" message:@"只能够最前面插入资料哦!!!" delegate:self cancelButtonTitle:@"取消" otherButtonTitles:nil, nil];
[al show];
}else{
pvc1.block3 = ^void (UIImage *image){
_count++;
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *file = [NSString stringWithFormat:@"%@",self.modelHome.customerName];
NSString *file1 = [NSString stringWithFormat:@"%@",_lageModel.nameStage];
NSString *file2 = [NSString stringWithFormat:@"%@",self.attName.text];
NSFileManager *fileManager = [NSFileManager defaultManager];
NSString *myString = [NSString stringWithFormat:@"/%@/%@/%@",file,file1,file2];
NSString *myDirectory = [documentsDirectory stringByAppendingPathComponent:myString];
NSArray *files = [fileManager subpathsOfDirectoryAtPath: myDirectory error:nil];
NSLog(@"files%@",files);
for (NSInteger i = 0 ; i < self.numDataSource.count; i ++) {
NSString *string = [NSString stringWithFormat:@"%@.jpg",self.numDataSource[i]];
NSLog(@"string%@",string);
for (NSString *str in files) {
NSRange range = [str rangeOfString:string];
if(range.location != NSNotFound)
{
NSString *str1 = [NSString stringWithFormat:@"%@/%@",myDirectory,str];
[self.insetDataArray addObject:str1];
}
}
}
NSString *lastString = [NSString stringWithFormat:@"/%@/%@/%@/%@_%ld.jpg",file,file1,file2,self.attName.text,([[self.numDataSource objectAtIndex:self.numDataSource.count-1] integerValue]+1)];
NSString *lastStringPath = [documentsDirectory stringByAppendingPathComponent:lastString];
[self.insetDataArray addObject:lastStringPath];
for (NSInteger i = self.insetDataArray.count-1 ; i > 0; i --) {
NSLog(@"self.insetDataArray[i]%@",self.insetDataArray[i]);
NSLog(@"self.insetDataArray[i]%@",self.insetDataArray[i-1]);
[fileManager moveItemAtPath:self.insetDataArray[i-1] toPath:self.insetDataArray[i] error:nil];
}
NSString *string = [NSString stringWithFormat:@"/%@/%@/%@/%@_%ld.jpg",file,file1,file2,self.attName.text,self.removeIndex];
NSLog(@"");
NSString *string1 = [documentsDirectory stringByAppendingPathComponent:string];
NSLog(@"string1%@",string1);
NSData *data = UIImageJPEGRepresentation(image,0.5);
[fileManager createFileAtPath:string1 contents:data attributes:nil];
};
[self presentViewController:pvc1 animated:YES completion:nil];
}
}
4.在某一个位置往后面插入一张照片
#pragma mark - 往后面插入一张照片
-(void)insetNext{
PhotoViewController *pvc = [[PhotoViewController alloc]init];
pvc.block3 = ^void (UIImage *image){
_count ++;
NSLog(@"插入钱一张");
[self.self.insetDataArray removeAllObjects];
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *file = [NSString stringWithFormat:@"%@",self.modelHome.customerName];
NSString *file1 = [NSString stringWithFormat:@"%@",_lageModel.nameStage];
NSString *file2 = [NSString stringWithFormat:@"%@",self.attName.text];
NSFileManager *fileManager = [NSFileManager defaultManager];
NSString *myString = [NSString stringWithFormat:@"/%@/%@/%@",file,file1,file2];
NSString *myDirectory = [documentsDirectory stringByAppendingPathComponent:myString];
NSArray *files = [fileManager subpathsOfDirectoryAtPath: myDirectory error:nil];
NSLog(@"files%@",files);
NSMutableArray *arr = [NSMutableArray array];
for (NSNumber *num in self.numDataSource) {
NSLog(@"numninin---%ld",(long)self.tempindex);
if ((long)[num integerValue] > (long)self.tempindex) {
NSLog(@"我进来");
[arr addObject:num];
}
}
for (NSInteger i = 0 ; i < arr.count; i ++) {
NSString *string = [NSString stringWithFormat:@"%@.jpg",arr[i]];
NSLog(@"string%@",string);
for (NSString *str in files) {
NSRange range = [str rangeOfString:string];
if(range.location != NSNotFound)
{
NSString *str1 = [NSString stringWithFormat:@"%@/%@",myDirectory,str];
[self.insetDataArray addObject:str1];
}
}
}
NSString *lastString = [NSString stringWithFormat:@"/%@/%@/%@/%@_%ld.jpg",file,file1,file2,self.attName.text,([[self.numDataSource objectAtIndex:self.numDataSource.count-1] integerValue]+1)];
NSString *lastStringPath = [documentsDirectory stringByAppendingPathComponent:lastString];
[self.insetDataArray addObject:lastStringPath];
NSLog(@"lastString%@",lastString);
NSLog(@"lastStringPath%@",lastStringPath);
NSLog(@"------%@",self.numDataSource);
NSLog(@"......%ld",[self.numDataSource indexOfObject:@(self.tempindex)]);
NSLog(@"self.insetDataArray[i]%@",self.insetDataArray);
for (NSInteger i = self.insetDataArray.count-1 ; i > 0; i --) {
NSLog(@"self.insetDataArray[i]%@",self.insetDataArray[i]);
NSLog(@"self.insetDataArray[i]%@",self.insetDataArray[i-1]);
[fileManager moveItemAtPath:self.insetDataArray[i-1] toPath:self.insetDataArray[i] error:nil];
}
NSArray *files1 = [fileManager subpathsOfDirectoryAtPath: myDirectory error:nil];
NSLog(@"files%@",files1);
NSString *string = [NSString stringWithFormat:@"/%@/%@/%@/%@_%ld.jpg",file,file1,file2,self.attName.text,self.removeIndex+1];
NSLog(@"");
NSString *string1 = [documentsDirectory stringByAppendingPathComponent:string];
NSLog(@"string1%@",string1);
NSData *data = UIImageJPEGRepresentation(image,0.5);
[fileManager createFileAtPath:string1 contents:data attributes:nil];
[self refreshImage];
};
[self presentViewController:pvc animated:YES completion:nil];
}
5调整两张图片的位置,改变其对应的目录
-(void)changePlace{
[UIView animateWithDuration:0.5 animations:^{
self.xuhaotextField = [[UITextField alloc]initWithFrame:CGRectMake(10, 5, WIDTH-20, 30)];
self.xuhaotextField.placeholder = @"请输入交换图片序号";
self.xuhaotextField.borderStyle = UITextBorderStyleRoundedRect;
[self.xuhaotextField addTarget:self action:@selector(fieldText:) forControlEvents:UIControlEventEditingChanged];
[self.changeView addSubview:self.xuhaotextField];
self.changeView.frame = CGRectMake(0, 220, WIDTH, 75);
self.xuhaotextField.height = 30;
self.button1 = [UIButton buttonWithType:UIButtonTypeSystem];
self.button1.frame = CGRectMake(5, CGRectGetMaxY(self.xuhaotextField.frame)+5, (WIDTH-20)/2, 30);
[self.button1 setTitle:@"取消" forState:UIControlStateNormal];
self.button1.backgroundColor = [UIColor blueColor];
[self.button1 addTarget:self action:@selector(btn1:) forControlEvents:UIControlEventTouchUpInside];
[self.changeView addSubview:self.button1];
self.button2 = [UIButton buttonWithType:UIButtonTypeSystem];
self.button2.frame = CGRectMake(CGRectGetMaxX(self.button1.frame)+10, CGRectGetMaxY(self.xuhaotextField.frame)+5, (WIDTH-20)/2, 30);
[self.button2 setTitle:@"确定" forState:UIControlStateNormal];
self.button2.backgroundColor = [UIColor blueColor];
[self.button2 addTarget:self action:@selector(btn2:) forControlEvents:UIControlEventTouchUpInside];
[self.changeView addSubview:self.button2];
}];
}
6文件重新命名,改变文件名字
#pragma mark - 文件重命名
-(void)againName{
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *file = [NSString stringWithFormat:@"%@",self.modelHome.customerName];
NSString *file1 = [NSString stringWithFormat:@"%@",_lageModel.nameStage];
NSString *file2 = [NSString stringWithFormat:@"%@",self.attName.text];
NSFileManager *fileManager = [NSFileManager defaultManager];
NSString *string = [NSString stringWithFormat:@"/%@/%@/%@",file,file1,file2];
NSString *string1 = [documentsDirectory stringByAppendingPathComponent:string];
NSArray *myfiles = [fileManager subpathsOfDirectoryAtPath: string1 error:nil];
NSString *stringMove;
for (NSString *str in myfiles) {
if ([str rangeOfString:[NSString stringWithFormat:@"%ld.jpg",self.removeIndex]].location !=NSNotFound ) {
NSLog(@"包涵");
NSString *mystring = str;
stringMove = [NSString stringWithFormat:@"/%@/%@/%@/%@",file,file1,file2,mystring];
}
}
NSString *myDirectory = [documentsDirectory stringByAppendingPathComponent:stringMove];
NSLog(@"文件存在");
self.myString = [NSString stringWithFormat:@"/%@/%@/%@/%@_%ld.jpg",file,file1,file2,self.textString,self.removeIndex];
NSString *myDirectory1 = [documentsDirectory stringByAppendingPathComponent:self.myString];
[fileManager moveItemAtPath:myDirectory toPath:myDirectory1 error:nil];
self.upLabel.text = self.textString;
_count++;
[self.promptView removeFromSuperview];
[self refreshImage];
}