我有一個AssetInfo對象,我用它來封裝關於我的應用中可能有的ALAsset或其他資產類型的基本信息。我有兩倍於儀器的CGImage對象數量比我應該多
資產信息將[alAsset thumbnail]保存爲CGImageRef。當我檢查文書時,我看起來有兩倍的CGImage對象。假設有4000個AssetInfo實例,我將擁有大約8000個CGImage實例。
我正在實例化我的AssetInfo對象,如下所示。
- (void)enumerateAsset:(AssetsGroupInfo *)assetsGroup alAsset:(ALAsset *)alAsset withIndex:(NSUInteger)index {
NSDate *date = [alAsset valueForProperty:ALAssetPropertyDate];
NSString *dateKey = [date dateKey];
CGImageRef thumbnail = [alAsset thumbnail];
NSURL *url = [[alAsset defaultRepresentation] url];
AssetInfo *alAssetInfo = [[AssetInfo alloc]
initWithURL:url
withDate:date
withThumbnail:thumbnail];
//...
}
我assetinfo的類看起來像這樣
@implementation AssetInfo {
@private
NSDate *_dateCreated;
NSURL *_url;
CGImageRef _thumbnail;
}
- (id)initWithURL:(NSURL *)url withDate:(NSDate *)date withThumbnail:(CGImageRef)thumbnailRef {
if ((self = [self init])) {
_url = url;
_dateCreated = date;
_thumbnail = CGImageRetain(thumbnailRef);
}
return self;
}
- (NSURL *)url {
return _url;
}
- (NSDate *)dateCreated {
return _dateCreated;
}
- (CGImageRef)thumbnail {
return _thumbnail;
}
#pragma mark - Equality
- (BOOL)isEqual:(id)object {
//TODO:Include date in equality checking
BOOL valueToReturn = NO;
if (object && [object respondsToSelector:@selector(url)]) {
NSURL *myUrl = [self url];
NSURL *objURL = [object url];
valueToReturn = ([myUrl isEqualToURL:objURL]);
}
return valueToReturn;
}
- (NSUInteger)hash {
return [[self url] hash];
}
- (void)dealloc {
NSLog(@"%s", __PRETTY_FUNCTION__);
CGImageRelease(_thumbnail);
}
@end
我後來用資產如下圖所示的代碼。
- (void)loadWithAsset:(AssetInfo *)asset {
_asset = asset;
_image = [[UIImage alloc] initWithCGImage:[_asset thumbnail]];
}
如果我釋放的縮略圖在我在哪裏實例化assetinfo的(如下面的snnippet)活CGImage對象的數量的點對應於活assetinfo的對象的數量。但是當我嘗試訪問AssetInfo對象的縮略圖屬性時,應用程序在上述代碼中使用EXC_BAD_ACCESS進行崩潰,其中我使用CGImageRef實例化了UIImage。
- (void)enumerateAsset:(AssetsGroupInfo *)assetsGroup alAsset:(ALAsset *)alAsset withIndex:(NSUInteger)index {
NSDate *date = [alAsset valueForProperty:ALAssetPropertyDate];
NSString *dateKey = [date dateKey];
CGImageRef thumbnail = [alAsset thumbnail];
NSURL *url = [[alAsset defaultRepresentation] url];
AssetInfo *alAssetInfo = [[AssetInfo alloc]
initWithURL:url
withDate:date
withThumbnail:thumbnail];
//...
CGImageRelease(thumbnail);
}
我做錯了什麼?
這一切都發生在我使用任何UIImage中的縮略圖之前。我只需枚舉資產庫中的資產並創建一個代表所有資產的對象圖。 –