在iPod上拍摄125张照片后,UIScrollView Photo Viewer收到内存警告

时间:2013-02-20 16:19:54

标签: iphone objective-c uiscrollview memory-warning photoviewer

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

我的要求类似于iphone的photoviewer应用程序,但还有更多内容,如上传图像,下载等。从技术上讲,我正在做的是创建一个主滚动视图,其内容大小等于所有选定的图像大小*图像宽度。我内部的小视图也等于否。从拾取器中选择的图像。该视图包含一个小的滚动视图,以及一个允许用户缩放,捏等的图像视图,主滚动视图具有允许滑动的手势列表器。 要访问图像,我存储资产URL并在用户向左或向右滑动时获取图像。在任何时间点,我只保留1个图像是内存,即如果用户滑动到第2张照片,则从主滚动视图中删除第1张照片。 / p>

但如果我选择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类作为singelton类吗?这会有帮助,因为我认为内存问题可能是因为创建了多个对象(虽然我正确地发布它)。

请帮帮我们。!! 提前谢谢。

1 个答案:

答案 0 :(得分:1)

您可以发布InnerScrollView的定义吗?最可能的问题是您的delegate属性为retain,这会使您的innerScrollViewObj无法取消分配。

所以,我认为你有类似的东西:

@property (nonatomic, retain) id delegate;

应该是:

@property (nonatomic) id delegate;

或者,如果您启用ARC,则应为:

@property (nonatomic, weak) id delegate;

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