如何发布从ALAssetsLibrary获取的UIimage

时间:2012-12-14 07:08:57

标签: objective-c uiimage release alassetslibrary

我花了整整2个星期才试图解决这个问题。太沮丧了!

我正在使用以下2个函数从设备库中获取图像。 如果我多次使用“setImage”函数,我会在iOS设备上丢失可用内存。

我认为“ [imageFromAsset initWithCGImage:[[myasset defaultRepresentation] fullScreenImage]]; ”在assetImage函数中会导致问题。

任何人都可以帮助我吗?任何线索或思考都会超级欣赏!请!

- (void)setImage:(NSURL *)imageURL{
UIImage *imageFromDeviceLibrary = [[UIImage alloc] init];
[[DevicePhotoControl sharedInstance] assetImage:[imageURL absoluteString] imageToStore:imageFromDeviceLibrary];    
UIImageView *fullImageView = [[UIImageView alloc] initWithImage:imageFromDeviceLibrary];
[imageFromDeviceLibrary release];
[self.view addSubview:fullImageView];
[fullImageView release];
}

- (void)assetImage:(NSString *)assetURL imageToStore:(UIImage *)imageFromAsset{
    // Handling exception case for when it doesn't have assets-library
if ([assetURL hasPrefix:@"assets-library:"] == NO) {
    assetURL = [NSString stringWithFormat:@"%@%@",@"assets-library:",assetURL];
}
__block BOOL busy = YES;

ALAssetsLibrary* assetslibrary = [[[ALAssetsLibrary alloc] init] autorelease];
//get image data by URL
ALAssetsLibraryAssetForURLResultBlock resultblock = ^(ALAsset *myasset)
{
    [imageFromAsset initWithCGImage:[[myasset defaultRepresentation] fullScreenImage]];
    busy = NO;
};
ALAssetsLibraryAccessFailureBlock failureblock  = ^(NSError *myerror)
{
    NSLog(@"Library Image Fetching Failed : %@",[myerror localizedDescription]);
    busy = NO;
};
[assetslibrary assetForURL:[NSURL URLWithString:assetURL]
               resultBlock:resultblock
              failureBlock:failureblock];

while (busy == YES) {
    [[NSRunLoop currentRunLoop] runMode:NSDefaultRunLoopMode beforeDate:[NSDate distantFuture]];
}
}

1 个答案:

答案 0 :(得分:1)

AssetLibrary发布时,所有资产对象都将随之消失。

我建议您在app委托中创建assetLibrary以使其保持活动状态,并且只有在收到来自ALAssetLibrary的更改通知ALAssetsLibraryChangedNotification

时才重置它

here

它可能对你有帮助。