So I m almost done with an app that is supposed to behave like a photo gallery. I m running into a novice issue though trying to properly delete photos. So, I have 3 UIImageViews. The images are stored in an NSMutableArray and loaded from it on startup. They delete and save perfectly. But, if I were to delete the image in UIImageView #2, then close and relaunch the app, the image from UIImageView #3 slides into #2 s spot. That s fine and everything, until I try and delete the image in #2 s spot or anything above #2 s.
我删除了以该名称为基础的照片,因此,如果我尝试并删除第2号《UIImageView》中的照片,我的代码将搜寻图像2.png并删除。 但是,现在当我重新启用时,UIImageView No3正在第2号现场,因此,当我尝试和删除UIImage第3号意见时,它不会删除,因为它在寻找已经不复存在的图像2.png。 该档案名为《形象3.png》。 因此,我稍感受损失,对如何将我的守则推向这一问题没有任何想法。 这里是Im公司目前使用的法典,这里的作者是<代码>。 NSString *filePath = [documents] Path stringByAppendingPathComponent:[NSString stringWithFormat:@ “Image%i.png”,view.tag]];。 (一) 每一门UIImage 书,并按此删除:
- (IBAction)deleteButtonPressed:(id)sender {
UIAlertView *deleteAlertView = [[UIAlertView alloc] initWithTitle:@"Delete"
message:@"Are you sure you want to delete this photo?"
delegate:self
cancelButtonTitle:@"No"
otherButtonTitles:@"Yes", nil];
[deleteAlertView show];
int imageIndex = ((UIButton *)sender).tag;
deleteAlertView.tag = imageIndex;
}
- (UIImageView *)viewForTag:(NSInteger)tag {
UIImageView *found = nil;
for (UIImageView *view in self.array) {
if (tag == view.tag) {
found = view;
break;
}
}
return found;
}
- (void)alertView: (UIAlertView *) alertView
clickedButtonAtIndex: (NSInteger) buttonIndex
{
if (buttonIndex != [alertView cancelButtonIndex]) {
UIImageView *view = [self viewForTag:alertView.tag];
if (view) {
[self.array removeObject:view];
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsPath = [paths objectAtIndex:0];
NSString *filePath = [documentsPath stringByAppendingPathComponent:[NSString stringWithFormat:@"Image%i.png",view.tag]];
NSFileManager *fileManager = [NSFileManager defaultManager];
NSError *error;
[fileManager removeItemAtPath:filePath error:&error];
if (error)
NSLog(@"Error: %@",error);
}
((UIImageView *)[self.view viewWithTag:alertView.tag]).image =nil;
}
[self.user setObject:self.array forKey:@"images"];
}
And here is my full .m file just for reference: https://github.com/Joey502/Test-Thing/wiki/.m-Class-File
如果任何人都知道如何解决如此大的问题!