【问题标题】:add file path to Mutable Array将文件路径添加到可变数组
【发布时间】:2014-05-07 05:04:16
【问题描述】:

我有一个绘图板应用程序,我正在尝试通过创建一个可变数组来制作一个撤消按钮,该数组将保存调用触摸开始时创建的图像的保存路径。这是我到目前为止的代码

-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {

t++;

mainImages = [[NSMutableArray alloc] init];

NSString *imagePath = [NSString stringWithFormat:@"Documents/imagePath%d.png", t];

savePath = [NSHomeDirectory() stringByAppendingPathComponent:imagePath];

[UIImagePNGRepresentation(_mainImage.image) writeToFile:savePath atomically:YES];


[mainImages addObject:savePath];

    NSLog(@"t is: %d", t);

    NSLog(@"imagePath is: %@", imagePath);

    NSLog(@"savePath is: %@", savePath);

    NSLog(@"contents of mainImages is: %@", mainImages);

mouseSwiped = NO;
UITouch *touch = [touches anyObject];
lastPoint = [touch locationInView:self.view];
UIGraphicsBeginImageContext(self.view.frame.size);
}

当我运行它时,我的新路径似乎没有添加到 Mutable 数组中,而是替换了之前保存的路径。这是我的调试控制台读取的内容:

2014-05-06 14:12:20.550 drawingSkills[6709:60b] t is: 1
2014-05-06 14:12:20.553 drawingSkills[6709:60b] imagePath is: Documents/imagePath1.png
2014-05-06 14:12:20.555 drawingSkills[6709:60b] savePath is: /var/mobile/Applications/9D3013C2-F275-486C-B1EF-8DAE9A5BEA91/Documents/imagePath1.png
2014-05-06 14:12:20.557 drawingSkills[6709:60b] contents of mainImages is: (
"/var/mobile/Applications/9D3013C2-F275-486C-B1EF-8DAE9A5BEA91/Documents/imagePath1.png"
)


2014-05-06 14:12:25.482 drawingSkills[6709:60b] t is: 2
2014-05-06 14:12:25.483 drawingSkills[6709:60b] imagePath is: Documents/imagePath2.png
2014-05-06 14:12:25.485 drawingSkills[6709:60b] savePath is: /var/mobile/Applications/9D3013C2-F275-486C-B1EF-8DAE9A5BEA91/Documents/imagePath2.png
2014-05-06 14:12:25.487 drawingSkills[6709:60b] contents of mainImages is: (
"/var/mobile/Applications/9D3013C2-F275-486C-B1EF-8DAE9A5BEA91/Documents/imagePath2.png"
)


2014-05-06 14:19:42.799 drawingSkills[6709:60b] t is: 3
2014-05-06 14:19:42.800 drawingSkills[6709:60b] imagePath is: Documents/imagePath3.png
2014-05-06 14:19:42.802 drawingSkills[6709:60b] savePath is: /var/mobile/Applications/9D3013C2-F275-486C-B1EF-8DAE9A5BEA91/Documents/imagePath3.png
2014-05-06 14:19:42.804 drawingSkills[6709:60b] contents of mainImages is: (
"/var/mobile/Applications/9D3013C2-F275-486C-B1EF-8DAE9A5BEA91/Documents/imagePath3.png"
)

谁能告诉我如何将路径添加到可变数组而不是替换以前的路径?

谢谢。

【问题讨论】:

  • 如果您提出其他问题,请确保正确格式化您的代码和/或日志输出。如果您让其他人难以阅读您的内容,您将不会得到很多答案。

标签: objective-c ios7 nsmutablearray


【解决方案1】:

每次调用 touchesBegan:withEvent: 时,代码中的以下行都会创建一个新数组,覆盖之前创建的数组:

mainImages = [[NSMutableArray alloc] init];

因此,数组不能有多个条目。您必须更改代码,以便数组只分配一次。我建议您将上面的行移至自定义视图类的初始化程序(通常为initWithFrame:)。

如果您绝对必须touchesBegan:withEvent: 内分配数组,请尝试以下相当粗略的解决方案:

static bool mainImagesAlreadyAllocated = false;
if (! mainImagesAlreadyAllocated)
{
  mainImages = [[NSMutableArray alloc] init];
  mainImagesAlreadyAllocated = true;
}

【讨论】:

  • mainImages != nil 将是一个更简单的测试。
  • 啊啊啊啊,是的!我现在看到了,很有道理。感谢您的帮助。
猜你喜欢
  • 1970-01-01
  • 2021-02-28
  • 1970-01-01
  • 1970-01-01
  • 2020-06-22
  • 2016-02-08
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多