2012-12-14 14 views
0

我花了整整2周才嘗試解決此問題。很沮喪!如何釋放從ALAssetsLibrary中獲取的UIimage

以下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

AssetLibrary發佈的那一刻,所有資產對象都將隨之消失。

我建議你創建的應用程序委託你的素材資源庫,以保持它活着,只有當你從ALAssetLibrary

here

收到變更通知ALAssetsLibraryChangedNotification它可以幫助你重新設置。

+0

非常感謝Girish!好吧,我現在要試一試,讓你知道! – Tommy

+0

我想知道何時觸發ALAssetsLibraryChangedNotification? – Tommy

+0

您需要添加通知以及刪除通知。 – Girish