0

我有一个 UIScrollView 显示来自 plist 文件的图像库。我还有一个从图库图像列表中删除图像的功能,该功能基本上删除了 plist 文件中的对象,然后在 ScrollView 中重新加载图像。问题是当我使用该方法时,我无法在重新加载新内容之前释放 UIScrollView 的图像- (IBAction)deleteimage:(id)sender。新内容已加载,但在旧内容之上,然后图像仍位于新内容后面。在重新加载滚动视图内容之前我应该​​怎么做才能释放图像?

我正在使用的代码是:

#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
4

3 回答 3

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
    }
}

如果您已经引用了图像视图,则可以直接在它们上调用方法,而无需遍历滚动视图的所有子视图。

于 2012-08-22T07:25:36.153 回答
1

您应该删除滚动视图中的所有 UIImageView。

就像是

NSArray* subviews = [[scrollview subviews] copy];
for (UIView* v in subviews) {
    [v removeFromSuperview];
}
[subviews release];
于 2012-08-22T07:20:36.273 回答
0

您忘记从 scrollView 子视图中删除以前的图像视图。对此(至少用于测试假设)的一种残酷方法是在开头添加以下行setupthescroll

[self.scrollView.subviews makeObjectsPerfomSelector:@selector(removeFromSuperview)];

这样做的问题是滚动视图的私有子视图(例如滚动条)也将被删除。因此,在实践中,您应该跟踪您在 ivar 数组中创建的子视图,并在该数组而不是子视图数组上执行上述行,然后清除该数组。

或者,一种更简洁的方法是仅删除与已删除图像对应的子视图,并更新剩余图像视图的帧,而不是删除并重新创建所有内容。您可以使用字典或 subviewstag属性来跟踪与哪个图像相关联的视图。

于 2012-08-22T07:34:31.853 回答