0

我陷入了一个奇怪的问题,试图从所有可能的来源中找到解决方案,但还没有运气。

我的要求类似于 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 类作为单格类吗?这会有帮助吗,因为我认为内存问题可能是因为创建了多个对象(尽管我正确地释放了它)。

请各位大侠帮忙!!提前致谢。

4

1 回答 1

1

你能发布 的定义InnerScrollView吗?最可能的问题是您的delegate属性是retain,这会阻止您的 innerScrollViewObj 被释放。

所以,我假设你有类似的东西:

@property (nonatomic, retain) id delegate;

应该是:

@property (nonatomic) id delegate;

或者,如果您启用 ARC,它应该是:

@property (nonatomic, weak) id delegate;

另外,请记住,[mainScrollView subviews]它只保存视图,而不是任何控制器。因此,如果您解除分配控制器,那么它将无法响应任何事件或控制任何内容,并且您可能会由于向解除分配的对象发送消息而出错。

于 2013-02-20T16:26:47.667 回答