0

我花了整整两周的时间来解决这个问题。太沮丧了!

以下 2 个函数是我用来从设备库中获取图像的函数。如果我多次使用“setImage”功能,我的 iOS 设备上的可用内存就会不断丢失。

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

任何人都可以帮助我吗?任何线索或想法都会非常感激!请!

- (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]];
}
}
4

1 回答 1

1

AssetLibrary 被释放的那一刻,所有的资产对象都将随之消失。

我建议您在应用程序委托中创建您的assetLibrary 以使其保持活动状态,并且仅在您从 ALAssetLibrary 收到更改通知 ALAssetsLibraryChangedNotification 时重置它

这里

它可能会帮助你。

于 2012-12-14T07:14:21.743 回答