[英]Object leak : Adding and removing UIImageView to a UIScrollView
[英]Removing UIImageView from UIScrollView
我知道已经有很多类似的问题问了。 请不要投票或关闭问题。 我刚刚尝试实施针对这些问题的解决方案。 什么都没有为我工作。 我的情况也略有不同。 它去了:
我的应用包含一个图片库。 它是一个滚动视图,显示从sqlite数据库检索的图片。 用户可以滚动图像,添加或删除图像等。我可以动态地向画廊添加图片。 但是问题出在我需要实现删除功能时。 我使用以下代码,但是即使在调用removeFromSuperView
,图像也不会从滚动视图中移除。
-(void)deleteDeck{
if(selectedEditDeck!=0){
[deck deleteSelectedDeck:selectedEditDeck]; //deleting from database
//problem starts here ***
[(UIImageView*)[decksGallery viewWithTag:selectedEditDeck-1]removeFromSuperview];
[self loadGallery];
selectedEditDeck=0;
//Ends here*****
[tableData release];
tableData=[NSMutableArray array];
[self showCardNamesinTable];
[aTableView reloadData];
}
我已经在loadview方法中创建了uiscrollview。 然后,在每次删除和添加图像后刷新视图,以便显示更新的图库,我使用以下代码:
-(void)loadGallery{ //Reloading all for adding a single deck image.
//Database part****
NSString *sqlStr = [NSString stringWithFormat:@"select first_card from decksTable"];
char *sql = (char*)[sqlStr UTF8String];
kidsFlashCardAppDelegate *appDelegate = [[UIApplication sharedApplication] delegate];
NSMutableArray *galleryImagesArray=[appDelegate.dbConnection fetchColumnFromTable:sql col:0];
NSArray *sysPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask,YES);
NSString *docDirectory = [sysPaths objectAtIndex:0];
numberofImages = [galleryImagesArray count];
printf("number is %d",numberofImages);//Database part of code ends here
//在以下代码片段中,我将图像添加到UIscrollView
for (int i = 0; i < [galleryImagesArray count]; i++) {
CGFloat yOrigin = i * 65;
NSString *filePath = [NSString stringWithFormat:@"%@/%@", docDirectory,[galleryImagesArray objectAtIndex:i]];
galleryImage = [[UIImageView alloc] initWithFrame:CGRectMake(yOrigin+140, 15,50,50 )];
//galleryImage.tag=[galleryImagesArray count];
galleryImage.tag=i;
printf("THE TAG IS %d",galleryImage.tag);
galleryImage.clipsToBounds=YES;
galleryImage.layer.cornerRadius=11.0;
galleryImage.backgroundColor=[UIColor clearColor];
galleryImage.image =[UIImage imageWithContentsOfFile:filePath];
[decksGallery addSubview:galleryImage];
[galleryImage release];
}
decksGallery.contentSize = CGSizeMake(115*[galleryImagesArray count], 80);
//[decksGallery reloadInputViews];
在运行刷新代码之前,请确保[decksGallery viewWithTag:selectedEditDeck-1]
不返回nil
并且实际上已从数据库中删除了该图像。
另外,您正在设置imageView.tag = i;
在创建代码中,由于i
为0(这是每个UIView的标记的默认值),因此最好也修复您的创建代码。
如果只想从imageView中删除图像,则也可以执行imageView.image = nil;。
UIImageView*imageView = (UIImageView*)[decksGallery viewWithTag:selectedEditDeck-1];
[imageView removeFromSuperview];
imageView = nil;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.