0

I am stuck in a weird problem,tried to find out solution from all possible sources but no luck yet.

My requirement is something similar to photoviewer app of iphone,but there is more to that like uploading image,downloading etc .So technically what i am doing is creating a main scrollview,which has content size equal to all the selected image size * image width .I have small view inside that is also equal to no. of images selected from picker .The view contains a small scrollview,and an image view which allows user to zoom,pinch etc and the main scroll view has gesture listner which allows to swipe. To access the images i am storing Asset URL and get the image when user swipes left or right .At any point in time I am keeping only 1 image is memory i.e if user swipe to 2nd photo i delete the 1st photo from main scrollview .

But still i am facing memory issues on iPod if i selected 125 photos and swipe multiple times to end and come back .

Sample Code.:-

    ***//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;
}

Should i make InnerImageView class as singelton class?Will this help,because i think the memory issues can be because multiple objects are getting created(though i am release it properly).

Please help guys.!! Thanks in advance.

Abhinandan Sahgal
  • 1,076
  • 1
  • 13
  • 27

1 Answers1

1

Can you post the definition of InnerScrollView? The most likely problem is that your delegate property is retain, which is keeping your innerScrollViewObj from being deallocated.

So, I assume you have something like:

@property (nonatomic, retain) id delegate;

Which should be:

@property (nonatomic) id delegate;

Or, if you enable ARC it should be:

@property (nonatomic, weak) id delegate;

Also, keep in mind that [mainScrollView subviews] only holds the views, not any controllers. So if you deallocate the controller then it will not be able to respond to any events or control anything, and you may get errors due to sending messages to deallocated objects.

Aaron Brager
  • 65,323
  • 19
  • 161
  • 287
  • :- Thanks for the Answer .I have modified the code with definition of inner scrollview. My delegate property is @property(assign) id delegate; I donot retain the delegate. – Abhinandan Sahgal Feb 20 '13 at 16:30
  • :-Correct,Inner scrollview is a view,so whenever i swipe left i delete the previous object of innerscrollview,whenever user swipe right it creates the object again based on position and display it,so i always have a view for the visible part. – Abhinandan Sahgal Feb 20 '13 at 16:34