gpt4 book ai didi

objective-c - 从 NSMutableArray 中删除图像

转载 作者:行者123 更新时间:2023-11-28 19:19:22 25 4
gpt4 key购买 nike

因此,我正在编写的应用程序允许用户从他们的相机胶卷上传照片,并将其显示在 UIImageView 中。我有一个“保存”按钮,可以在按下时保存图像。我还有一个“编辑”按钮,当点击时,它允许用户点击照片并将其删除。这是我遇到问题的地方,图像从 View 中删除,但似乎没有从数组中删除。当我关闭并重新启动应用程序时,照片再次出现。我是 Objective-C 的新手,我很惊讶我竟然做到了这一步,所以我在弄清楚如何从数组中删除它时遇到了问题。

此外,作为旁注,如果我从相机胶卷中抓取 3 张图像,删除中间的一张,然后按保存按钮,在关闭并重新启动应用程序后,它只显示应有的 2 张照片。只有当我先按保存,然后尝试删除它们时,才会出现有关图像在重新启动后再次出现的问题。

这就是我删除它们的方式。我在每个 UIImageView 上都有一个不可见的按钮,当按下编辑按钮时,它会取消隐藏它们。我已经标记了按钮和 UIImageViews,当按下其中一个按钮时,将触发:

- (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];
}

((UIImageView *)[self.view viewWithTag:alertView.tag]).image =nil;
}

[self.user setObject:self.array forKey:@"images"];
}

仅供引用,这就是我保存图像的方式:

    - (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingImage:(UIImage *)img editingInfo:(NSDictionary *)editInfo {
if (imageView.image == nil) {
imageView.image = img;

[self.array addObject:imageView];

[picker dismissModalViewControllerAnimated:YES];
[self.popover dismissPopoverAnimated:YES];

return;

}

if (imageView2.image == nil) {
imageView2.image = img;
NSLog(@"The image is a %@", imageView);
[self.array addObject:imageView2];

[picker dismissModalViewControllerAnimated:YES];
[self.popover dismissPopoverAnimated:YES];

return;
}
...
...
}

-(IBAction)saveButtonPressed:(id)sender {
NSString *docsDir = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask,YES) objectAtIndex:0];



for (UIImageView *imageViewToSave in self.array) {

NSInteger tag = imageViewToSave.tag;
UIImage *image = imageViewToSave.image;
NSString *imageName = [NSString stringWithFormat:@"Image%i.png",tag];

NSString *imagePath = [docsDir stringByAppendingPathComponent:imageName];

[UIImagePNGRepresentation(image) writeToFile:imagePath atomically:NO];
}

}

这就是我加载它们的方式:

    -(void)viewDidLoad {

self.array = [NSMutableArray array];
NSString *docsDir = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask,YES) objectAtIndex:0];

NSArray *docFiles = [[NSFileManager defaultManager]contentsOfDirectoryAtPath:docsDir error:NULL];

for (NSString *fileName in docFiles) {

if ([fileName hasSuffix:@".png"]) {
NSString *fullPath = [docsDir stringByAppendingPathComponent:fileName];
UIImage *loadedImage = [UIImage imageWithContentsOfFile:fullPath];

if (!imageView.image) {
imageView.image = loadedImage;
}
else if (!imageView2.image) {
imageView2.image = loadedImage;
}
else if (!imageView3.image) {
imageView3.image = loadedImage;

}

}
}
}

更新:这是我当前的代码:

- (IBAction)deleteButtonPressed:(id)sender {
NSLog(@"Sender is %@", 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];
[fileManager removeItemAtPath:filePath error:NULL];
}

((UIImageView *)[self.view viewWithTag:alertView.tag]).image =nil;
}

[self.user setObject:self.array forKey:@"images"];
}

最佳答案

当您从数组 [self.array removeObject:view]; 中删除图像时,您并未从目录中删除图像本身。

array 中删除对象后,您可能想做这样的事情:

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);  
NSString *documentsPath = [paths objectAtIndex:0];
NSString *filePath = [documentsPath stringByAppendingPathComponent:@"THE_IMAGE_NAME_HERE.png"];
NSFileManager *fileManager = [NSFileManager defaultManager];
NSError *error;
[fileManager removeItemAtPath:filePath error:&error];

if (error)
NSLog(@"Error: %@",error);

关于objective-c - 从 NSMutableArray 中删除图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10168135/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com