English 中文(简体)
消除适当的形象
原标题:Deleting the appropriate image

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

如果任何人都知道如何解决如此大的问题!

最佳回答

你们想要做的是再创建<代码>NSMutableArray,称作NamesArray>/code>,储存与每种(按同一顺序)有关的档案名称。 意见储存在<代码>上。

在发送传真时,请将每一文档名称(NSStrings)填入<代码>。 UIImageViews to sol.array。 然后,在你删除<代码>时,在你删除该编号。 UIImageView Object from sol.array and abolition from the行文, 还请将文档名称自。 因此,一旦物体从两个阵列中删除,所有物体将按相同数额转移,每个档案名称均符合正确的图像观点。

仅补充一点:在删除图像时,你应使用<条码>><>>>> 标/代码>从<条码>上检索文档名称。 您删除职能的例子如下:

if (view) {
    [self.array removeObject:view]; //or you could do [self.array removeObjectAtIndex:alertView.tag];

    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);  
    NSString *documentsPath = [paths objectAtIndex:0];

    NSString *filePath = [documentsPath stringByAppendingPathComponent:[self.fileNamesArray objectAtIndex:alertView.tag]];
    NSFileManager *fileManager = [NSFileManager defaultManager];

    NSError *error;

    [fileManager removeItemAtPath:filePath error:&error];

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

    [self.fileNamesArray removeObjectAtIndex: alertView.tag];
}

www.un.org/spanish/ecosoc 处理评论意见中提出的问题的最新情况:

  1. 在您的<条码>应用DidEnterBackground方法中,您再次将所有图像观点纳入<条码>。 如果阵列空什么? 然后,你只再补充所需物品。 这种方法中唯一需要的是最后两条。

  2. http://www.ohchr.org。 缩略语 View。 移除在<代码>后添加图像观点的所有线。 页: 1 图像=[UIImage 图像WithContentsOfFile:fullPath]; 添加这一行码<>。

  3. In your delete function, this line is missing two brackets: NSString *filePath = [documentsPath stringByAppendingPathComponent:[self.fileNamesArray objectAtIndex:alertView.tag;. It should be NSString *filePath = [documentsPath stringByAppendingPathComponent:[self.fileNamesArray objectAtIndex:alertView.tag]];. I m wondering if this is why your whole directory is being deleted.

  4. Don tabe, 当你在图像采集器上添加图像时,你还应在(正确顺序)的选人代表法中增加的文档名称。

问题回答

Check your use of self.array. Sometimes you are using it to store/retrieve UIImageView s, other places you are storing/retrieving the images from the image views.

If you are trying to use self.array for two different purposes, then you either need to release and allocate a new one when switching purposes, or remove all the current contents before switching to the other purpose.





相关问题
List Contents of Directory in a UITableView

I am trying to list the contents of Ringtones directory in a TableView, however, I am only getting the last file in the directory in ALL cells, instead of file per cell. This is my code: - (...

iPhone NSUserDefaults persistance difficulty

In my app i have a bunch of data i store in the NSUserdefaults. This information consists of an NSObject (Object1) with NSStrings and NSNumbers and also 2 instances of yet another object (Object2). ...

Writing a masked image to disk as a PNG file

Basically I m downloading images off of a webserver and then caching them to the disk, but before I do so I want to mask them. I m using the masking code everyone seems to point at which can be found ...

Resize UIImage with aspect ratio?

I m using this code to resize an image on the iPhone: CGRect screenRect = CGRectMake(0, 0, 320.0, 480.0); UIGraphicsBeginImageContext(screenRect.size); [value drawInRect:screenRect blendMode:...

Allowing interaction with a UIView under another UIView

Is there a simple way of allowing interaction with a button in a UIView that lies under another UIView - where there are no actual objects from the top UIView on top of the button? For instance, ...

热门标签