我有一个UIScrollView,它显示了plist文件中的图像库。
我还有一个从库图像列表中删除图像的功能,它基本上删除了plist文件中的对象,然后在ScrollView中重新加载图像。
问题是,当我使用方法- (IBAction)deleteimage:(id)sender
时,我无法释放UIScrollView的图像,然后使用新内容重新加载它。新内容已加载,但在旧内容上,然后图像仍然在新内容后面。
在重新加载scrollview内容之前我应该怎么做才能发布图像?
我使用的代码是:
#import "ImageScrollViewController.h"
@interface ImageScrollViewController ()
@end
@implementation ImageScrollViewController
@synthesize images,scrollView,pageControl,subview;
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view from its nib.
scrollView.autoresizingMask = ( UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight);
[self setupthescroll];
self.pageControl.currentPage = 0;
}
- (void)setupthescroll{
//Get the images of the Array
NSUserDefaults *success = [NSUserDefaults standardUserDefaults];
images = [success mutableArrayValueForKey:@"imagelist"];
NSLog(@"list of images%@",images);
pageControlBeingUsed = NO;
for (int i = 0; i < images.count; i++) {
CGRect frame;
frame.origin.x = self.scrollView.frame.size.width * i;
frame.origin.y = 0;
frame.size = self.scrollView.frame.size;
subview = [[UIImageView alloc] initWithFrame:frame];
NSString *str4 = [images objectAtIndex:i];
subview.image = [[[UIImage alloc] initWithContentsOfFile:str4] autorelease];
self.subview.contentMode = UIViewContentModeScaleAspectFit;
[self.scrollView addSubview:subview];
[subview release];
}
self.scrollView.contentSize = CGSizeMake(self.scrollView.frame.size.width * images.count, self.scrollView.frame.size.height);
self.pageControl.numberOfPages = images.count;
//Get the number of the images
int page;
page = self.pageControl.currentPage;
printf("Current Page: %d", page);
}
- (void)scrollViewDidScroll:(UIScrollView *)sender {
if (!pageControlBeingUsed) {
// Switch the indicator when more than 50% of the previous/next page is visible
CGFloat pageWidth = self.scrollView.frame.size.width;
int page = floor((self.scrollView.contentOffset.x - pageWidth / 2) / pageWidth) + 1;
self.pageControl.currentPage = page;
}
}
- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView {
pageControlBeingUsed = NO;
}
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView {
pageControlBeingUsed = NO;
}
- (IBAction)changePage {
// Update the scroll view to the appropriate page
CGRect frame;
frame.origin.x = self.scrollView.frame.size.width * self.pageControl.currentPage;
frame.origin.y = 0;
frame.size = self.scrollView.frame.size;
[self.scrollView scrollRectToVisible:frame animated:YES];
pageControlBeingUsed = YES;
}
- (IBAction)deleteimage:(id)sender{
//Get the number of the image
int page;
page = self.pageControl.currentPage;
printf("Current Page: %d", page);
//Remove the images of the Array
NSUserDefaults *success = [NSUserDefaults standardUserDefaults];
images = [success mutableArrayValueForKey:@"imagelist"];
[images removeObjectAtIndex:page];
NSLog(@"list of images%@",images);
//Update the Array
NSUserDefaults *arrayofimages = [NSUserDefaults standardUserDefaults];
[arrayofimages setObject:images forKey:@"imagelist"];
//Refresh the ScrollView
[self setupthescroll];
//post the notification than images have been updated
NSUserDefaults *deleted = [NSUserDefaults standardUserDefaults];
[deleted setObject:@"deleted" forKey:@"deletedimages"];
}
- (IBAction)closepage:(id)sender{
[self dismissModalViewControllerAnimated:YES];
}
- (void)viewDidUnload
{
[super viewDidUnload];
// Release any retained subviews of the main view.
// e.g. self.myOutlet = nil;
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
- (void)dealloc {
[pageControl release];
[scrollView release];
//[images release];
[super dealloc];
}
@end
答案 0 :(得分:2)
如果要重新加载整个滚动视图(即重新添加所有图像),首先需要删除现有图像。
使用UIView的 removeFromSuperview 方法从滚动视图中删除视图。
因此,从滚动视图中删除所有图像视图的代码段看起来就像这样
NSArray *imgViews = [scrollView subviews];
for(id aView in imgViews){
if([aView isKindOfClass:[UIImageView class]]){
[aView removeFromSuperview]; //remove only if its an imageview
}
}
如果您已经引用了图像视图,则可以直接调用它们上的方法,而无需遍历scrollview的所有子视图。
答案 1 :(得分:1)
您应该删除scrollView中的所有UIImageView。
类似
NSArray* subviews = [[scrollview subviews] copy];
for (UIView* v in subviews) {
[v removeFromSuperview];
}
[subviews release];
答案 2 :(得分:0)
您忘记从scrollView子视图中删除以前的图像视图。一个残酷的方法(至少用于测试假设)将在setupthescroll
的开头添加以下行:
[self.scrollView.subviews makeObjectsPerfomSelector:@selector(removeFromSuperview)];
这样做的问题是,scrollview的私有子视图(例如,滚动条)也将被删除。 所以在实践中你应该跟踪你在ivar数组中创建的子视图,并在这个数组而不是subviews数组上执行上面的行,然后清除数组。
或者,更干净的方法是仅删除与删除的图像相对应的子视图,并更新剩余图像视图的帧,而不是删除和重新创建所有内容。您可以使用字典或子视图tag
属性来跟踪哪个视图与哪个图像相关联。