我陷入了一个奇怪的问题,试图从所有可能的来源中找到解决方案,但还没有运气。
我的要求类似于 iphone 的 photoviewer 应用程序,但还有更多内容,例如上传图像、下载等。所以从技术上讲,我正在做的是创建一个主滚动视图,其内容大小等于所有选定的图像大小 * 图像宽度.I have small view inside 那也等于没有。从选取器中选择的图像。该视图包含一个小的滚动视图和一个允许用户缩放、捏合等的图像视图,主滚动视图具有允许滑动的手势侦听器。要访问图像,我正在存储资产 URL 并在用户向左或向右滑动时获取图像。在任何时候,我只保留 1 张图像是内存,即如果用户滑动到第二张照片,我会从主滚动视图中删除第一张照片。
但是,如果我选择了 125 张照片并多次滑动以结束并返回,我仍然面临 iPod 上的内存问题。
示例代码:-
***//InnerScrollView is the class which contains view with scroll and imageview
//The values of initial and final counter will always be a diffrence of 1 so it creates only one object**
for(int j=initialCounter;j<finalCounter;j++) {
InnerScrollView *innerScrollViewObj=[[InnerScrollView alloc] initWithFrame:CGRectMake(320*j, 0, 320, 480)
url:[selectedImageList objectAtIndex:j] tag:self.initialImageViewCounter];
innerScrollViewObj.delegate=self;
[mainScrollView addSubview:innerScrollViewObj];
[innerScrollViewObj release];
innerScrollViewObj=nil;
}
**//This is main scrollview which gets created once**
mainScrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0,0 , 320, 480)];
mainScrollView.pagingEnabled = YES;
// mainScrollView.delegate=self;
mainScrollView.showsHorizontalScrollIndicator = NO;
mainScrollView.showsVerticalScrollIndicator = NO;
mainScrollView.maximumZoomScale = 4.0;
mainScrollView.minimumZoomScale = 1.0;
mainScrollView.backgroundColor=[UIColor blackColor];
[mainScrollView setCanCancelContentTouches:NO];
mainScrollView.clipsToBounds = YES;
mainScrollView.scrollEnabled = FALSE;
mainScrollView.contentSize = CGSizeMake(320*[selectedImageList count], mainScrollView.bounds.size.height);
[self.view addSubview:mainScrollView];
//THis is the method which gets called when i swipe left and the previous images gets deleted
-(void)deleteScrollViewFormMainView:(NSNumber*)indexToDelete{
@try{
NSLog(@"ScrollView to be deleted %@",[[mainScrollView subviews] objectAtIndex:0]);
[[[mainScrollView subviews] objectAtIndex:0] removeFromSuperview];
}
@catch (NSException *e) {
NSLog(@"ScrollView view exception %@",[e description]);
}
}
- (id)initWithFrame:(CGRect)frame url :(ALAsset*) imageUrl tag:(int)tag
{
self = [super initWithFrame:frame];
if (self) {
self.userInteractionEnabled=YES;
// self.tag=tag;
UIImage *image = [UIImage imageWithCGImage:[[imageUrl defaultRepresentation] fullResolutionImage ]];
UIImageView* imageview = [[UIImageView alloc] initWithImage:image];
imageview.tag = VIEW_FOR_ZOOM_TAG;
imageview.frame=CGRectMake(0, 0, self.frame.size.width, self.frame.size.height);
imageview.backgroundColor=[UIColor clearColor];
imageview.contentMode=UIViewContentModeScaleAspectFit;
UIScrollView* pageScrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, self.frame.size.width, self.frame.size.height)];
NSLog(@"Page scrollview is %@",pageScrollView);
pageScrollView.tag=tag;
pageScrollView.minimumZoomScale = 0.4f;
pageScrollView.maximumZoomScale = 2.0f;
pageScrollView.zoomScale = 1.0f;
pageScrollView.backgroundColor=[UIColor clearColor];
pageScrollView.delegate = self;
pageScrollView.showsHorizontalScrollIndicator = NO;
pageScrollView.showsVerticalScrollIndicator = NO;
[pageScrollView setCanCancelContentTouches:NO];
[self addSubview:pageScrollView];
[pageScrollView addSubview:imageview];
UITapGestureRecognizer *singleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleSingleTap)];
singleTap.numberOfTapsRequired=1;
singleTap.delegate=self;
[self addGestureRecognizer:singleTap];
singleTap=nil;
UITapGestureRecognizer *doubleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleDoubleTap)];
doubleTap.numberOfTapsRequired=2;
doubleTap.delegate=self;
[self addGestureRecognizer:doubleTap];
singleTap=nil;
doubleTap=nil;
imageview=nil;
pageScrollView=nil;
}
return self;
}
我应该将 InnerImageView 类作为单格类吗?这会有帮助吗,因为我认为内存问题可能是因为创建了多个对象(尽管我正确地释放了它)。
请各位大侠帮忙!!提前致谢。