iOS:快速滚动照片时出错?

时间:2014-02-18 00:48:51

标签: ios objective-c uiimageview didreceivememorywarning mwphotobrowser

我目前正在我的应用中使用MWPhotoBrowser,当我快速滚动图像时出现以下错误:

Received memory warning.
2014-02-17 16:42:35.117 App[10803:60b] *** Terminating app due to uncaught exception 'NSGenericException', reason: '*** Collection <__NSArrayM: 0x156f3160> was mutated while being enumerated.'
*** First throw call stack:
(0x2e71de83 0x38a7a6c7 0x2e71d971 0x151167 0x15139b 0x311643ff 0x3116446f 0x31164665 0x31164805 0x3111ea67 0x38f5f0af 0x38f6072f 0x38f61959 0x2e6e85b1 0x2e6e6e7d 0x2e651471 0x2e651253 0x3338b2eb 0x30f06845 0xff035 0x38f73ab7)
libc++abi.dylib: terminating with uncaught exception of type NSException

我目前正在本地加载存储在应用中的图片。

这是抛出异常的方法:

- (void)releaseAllUnderlyingPhotos:(BOOL)preserveCurrent {
    for (id p in _photos) {
        if (p != [NSNull null]) {
            if (preserveCurrent && p == [self photoAtIndex:self.currentIndex]) {
                continue; // skip current
            }
            [p unloadUnderlyingImage];
        }
    } // Release photos
}

非常感谢任何帮助!

1 个答案:

答案 0 :(得分:1)

我不确切知道您正在使用哪个版本的MWPhotoBrowser,但在最新版本here中,releaseAllUnderlyingPhotos:方法显示为:

- (void)releaseAllUnderlyingPhotos:(BOOL)preserveCurrent {
    // Create a copy in case this array is modified while we are looping through
    // Release photos
    NSArray *copy = [_photos copy];
    for (id p in copy) {
        if (p != [NSNull null]) {
            if (preserveCurrent && p == [self photoAtIndex:self.currentIndex]) {
                continue; // skip current
            }
            [p unloadUnderlyingImage];
        }
    }
    // Release thumbs
    copy = [_thumbPhotos copy];
    for (id p in copy) {
        if (p != [NSNull null]) {
            [p unloadUnderlyingImage];
        }
    }
}

请注意以下两行:

    NSArray *copy = [_photos copy];
    for (id p in copy) {

在迭代_photos之前,创建了一个新数组copy,以保护此迭代不被_photos在其他地方修改

在您的代码中,releaseAllUnderlyingPhotos:正在直接从_photos数组中删除对象,但可以在代码的其他部分修改此数组,例如didReceiveMemoryWarning,因为它是你的情况。

修改代码以迭代_photosreleaseAllUnderlyingPhotos:的副本后,您的问题就会消失。