开发者

Getting "Received memory warning. Level = 1"

Im getting this warning when im dealing with some images. What can i do?

Thanks!

        - (void)viewDidLoad {


            // Insere a view de thumbnails das paginas

            [self.view addSubview:pageThumbnails];
            self.pageThumbnails.frame = CGRectMake(0, 1024, 768, 210);

            // Paginas

            self.galeria =[[NSMutableArray alloc]init];

            [self.galeria addObject:[[NSBundle mainBundle] pathForResource:@"001" ofType:@"jpg"]];

            [self.galeria addObject:[[NSBundle mainBundle] pathForResource:@"002" ofType:@"jpg"]];

            [self.galeria addObject:[[NSBundle mainBundle] pathForResource:@"003" ofType:@"jpg"]];

            [self.galeria addObject:[[NSBundle mainBundle] pathForResource:@"004" ofType:@"jpg"]];

            [self.galeria addObject:[[NSBundle mainBundle] pathForResource:@"005" ofType:@"jpg"]];

            [self.galeria addObject:[[NSBundle mainBundle] pathForResource:@"006" ofType:@"jpg"]];

            [self.galeria addObject:[[NSBundle mainBundle] pathForResource:@"007" ofType:@"jpg"]];

            [self.galeria addObject:[[NSBundle mainBundle] pathForResource:@"008" ofType:@"jpg"]];

            [self.galeria addObject:[[NSBundle mainBundle] pathForResource:@"009" ofType:@"jpg"]];

            [self.galeria addObject:[[NSBundle mainBundle] pathForResource:@"010" ofType:@"jpg"]];

            [self.galeria addObject:[[NSBundle mainBundle] pathForResource:@"011" ofType:@"jpg"]];

            [self.galeria addObject:[[NSBundle mainBundle] pathForResource:@"012" ofType:@"jpg"]];

            [self.galeria addObject:[[NSBundle mainBundle] pathForResource:@"013" ofType:@"jpg"]];

            [self.galeria addObject:[[NSBundle mainBundle] pathForResource:@"014" ofType:@"jpg"]];

            [self.galeria addObject:[[NSBundle mainBundle] pathForResource:@"015" ofType:@"jpg"]];

            [self.galeria addObject:[[NSBundle mainBundle] pathForResource:@"016" ofType:@"jpg"]];

            [self.galeria addObject:[[NSBundle mainBundle] pathForResource:@"017" ofType:@"jpg"]];

            [self.galeria addObject:[[NSBundle mainBundle] pathForResource:@"018" ofType:@"jpg"]];

            [self.galeria addObject:[[NSBundle mainBundle] pathForResource:@"019" ofType:@"jpg"]];

            [self.galeria addObject:[[NSBundle mainBundle] pathForResource:@"020" ofType:@"jpg"]];

            [self.galeria addObject:[[NSBundle mainBundle] pathForResource:@"021" ofType:@"jpg"]];

            [self.galeria addObject:[[NSBundle mainBundle] pathForResource:@"022" ofType:@"jpg"]];

            [self.galeria addObject:[[NSBundle mainBundle] pathForResource:@"023" ofType:@"jpg"]];

            [self.galeria addObject:[[NSBundle mainBundle] pathForResource:@"024" ofType:@"jpg"]];

            [self.galeria addObject:[[NSBundle mainBundle] pathForResource:@"025" ofType:@"jpg"]];

            [self.galeria addObject:[[NSBundle mainBundle] pathForResource:@"026" ofType:@"jpg"]];

            [self.galeria addObject:[[NSBundle mainBundle] pathForResource:@"027" ofType:@"jpg"]];

            [self.galeria addObject:[[NSBundle mainBundle] pathForResource:@"028" ofType:@"jpg"]];

            [self.galeria addObject:[[NSBundle mainBundle] pathForResource:@"029" ofType:@"jpg"]];

            [self.galeria addObject:[[NSBundle mainBundle] pathForResource:@"030" ofType:@"jpg"]];

            [self.galeria addObject:[[NSBundle mainBundle] pathForResource:@"031" ofType:@"jpg"]];

            [self.galeria addObject:[[NSBundle mainBundle] pathForResource:@"032" ofType:@"jpg"]];

            [self.galeria addObject:[[NSBundle mainBundle] pathForResource:@"033" ofType:@"jpg"]];

            [self.galeria addObject:[[NSBundle mainBundle] pathForResource:@"034" ofType:@"jpg"]];

            [self.galeria addObject:[[NSBundle mainBundle] pathForResource:@"035" ofType:@"jpg"]];
            [self.galeria addObject:[[NSBundle mainBundle] pathForResource:@"036" ofType:@"jpg"]];

            [self.galeria addObject:[[NSBundle mainBundle] pathForResource:@"037" ofType:@"jpg"]];

            [self.galeria addObject:[[NSBundle mainBundle] pathForResource:@"038" ofType:@"jpg"]];

            [self.galeria addObject:[[NSBundle mainBundle] pathForResource:@"039" ofType:@"jpg"]];

            [self.galeria addObject:[[NSBundle mainBundle] pathForResource:@"040" ofType:@"jpg"]];

            [self.galeria addObject:[[NSBundle mainBundle] pathForResource:@"041" ofType:@"jpg"]];

            [self.galeria addObject:[[NSBundle mainBundle] pathForResource:@"042" ofType:@"jpg"]];

            [self.galeria addObject:[[NSBundle mainBundle] pathForResource:@"043" ofType:@"jpg"]];

            [self.galeria addObject:[[NSBundle mainBundle] pathForResource:@"044" ofType:@"jpg"]];

            [self.galeria addObject:[[NSBundle mainBundle] pathForResource:@"045" ofType:@"jpg"]];

            [self.galeria addObject:[[NSBundle mainBundle] pathForResource:@"046" ofType:@"jpg"]];

            [self.galeria addObject:[[NSBundle mainBundle] pathForResource:@"047" ofType:@"jpg"]];

            [self.galeria addObject:[[NSBundle mainBundle] pathForResource:@"048" ofType:@"jpg"]];

            [self.galeria addObject:[[NSBundle mainBundle] pathForResource:@"049" ofType:@"jpg"]];

            [self.galeria addObject:[[NSBundle mainBundle] pathForResource:@"050" ofType:@"jpg"]];

            [self.galeria addObject:[[NSBundle mainBundle] pathForResource:@"051" ofType:@"jpg"]];

            [self.galeria addObject:[[NSBundle mainBundle] pathForResource:@"052" ofType:@"jpg"]];

            [self.galeria addObject:[[NSBundle mainBundle] pathForResource:@"053" ofType:@"jpg"]];

            [self.galeria addObject:[[NSBundle mainBundle] pathForResource:@"054" ofType:@"jpg"]];

            [self.galeria addObject:[[NSBundle mainBundle] pathForResource:@"055" ofType:@"jpg"]];

            [self.galeria addObject:[[NSBundle mainBundle] pathForResource:@"056" ofType:@"jpg"]];

            [self.galeria addObject:[[NSBundle mainBundle] pathForResource:@"057" ofType:@"jpg"]];

            [self.galeria addObject:[[NSBundle mainBundle] pathForResource:@"058" ofType:@"jpg"]];

            [self.galeria addObject:[[NSBundle mainBundle] pathForResource:@"059" ofType:@"jpg"]];







            //Thumbs

            self.thumbsList =[[NSMutableArray alloc]init];

            [self.thumbsList addObject:[[NSBundle mainBundle] pathForResource:@"001_thumb" ofType:@"jpg"]];

            [self.thumbsList addObject:[[NSBundle mainBundle] pathForResource:@"002_thumb" ofType:@"jpg"]];

            [self.thumbsList addObject:[[NSBundle mainBundle] pathForResource:@"003_thumb" ofType:@"jpg"]];

            [self.thumbsList addObject:[[NSBundle mainBundle] pathForResource:@"004_thumb" ofType:@"jpg"]];

            [self.thumbsList addObject:[[NSBundle mainBundle] pathForResource:@"005_thumb" ofType:@"jpg"]];

            [self.thumbsList addObject:[[NSBundle mainBundle] pathForResource:@"006_thumb" ofType:@"jpg"]];

            [self.thumbsList addObject:[[NSBundle mainBundle] pathForResource:@"007_thumb" ofType:@"jpg"]];

            [self.thumbsList addObject:[[NSBundle mainBundle] pathForResource:@"008_thumb" ofType:@"jpg"]];

            [self.thumbsList addObject:[[NSBundle mainBundle] pathForResource:@"009_thumb" ofType:@"jpg"]];

            [self.thumbsList addObject:[[NSBundle mainBundle] pathForResource:@"010_thumb" ofType:@"jpg"]];

            [self.thumbsList addObject:[[NSBundle mainBundle] pathForResource:@"011_thumb" ofType:@"jpg"]];

            [self.thumbsList addObject:[[NSBundle mainBundle] pathForResource:@"012_thumb" ofType:@"jpg"]];

            [self.thumbsList addObject:[[NSBundle mainBundle] pathForResource:@"013_thumb" ofType:@"jpg"]];

            [self.thumbsList addObject:[[NSBundle mainBundle] pathForResource:@"014_thumb" ofType:@"jpg"]];

            [self.thumbsList addObject:[[NSBundle mainBundle] pathForResource:@"015_thumb" ofType:@"jpg"]];

            [self.thumbsList addObject:[[NSBundle mainBundle] pathForResource:@"016_thumb" ofType:@"jpg"]];

            [self.thumbsList addObject:[[NSBundle mainBundle] pathForResource:@"017_thumb" ofType:@"jpg"]];

            [self.thumbsList addObject:[[NSBundle mainBundle] pathForResource:@"018_thumb" ofType:@"jpg"]];

            [self.thumbsList addObject:[[NSBundle mainBundle] pathForResource:@"019_thumb" ofType:@"jpg"]];

            [self.thumbsList addObject:[[NSBundle mainBundle] pathForResource:@"020_thumb" ofType:@"jpg"]];

            [self.thumbsList addObject:[[NSBundle mainBundle] pathForResource:@"021_thumb" ofType:@"jpg"]];

            [self.thumbsList addObject:[[NSBundle mainBundle] pathForResource:@"022_thumb" ofType:@"jpg"]];

            [self.thumbsList addObject:[[NSBundle mainBundle] pathForResource:@"023_thumb" ofType:@"jpg"]];

            [self.thumbsList addObject:[[NSBundle mainBundle] pathForResource:@"024_thumb" ofType:@"jpg"]];

            [self.thumbsList addObject:[[NSBundle mainBundle] pathForResource:@"025_thumb" ofType:@"jpg"]];

            [self.thumbsList addObject:[[NSBundle mainBundle] pathForResource:@"026_thumb" ofType:@"jpg"]];

            [self.thumbsList addObject:[[NSBundle mainBundle] pathForResource:@"027_thumb" ofType:@"jpg"]];

            [self.thumbsList addObject:[[NSBundle mainBundle] pathForResource:@"028_thumb" ofType:@"jpg"]];

            [self.thumbsList addObject:[[NSBundle mainBundle] pathForResource:@"029_thumb" ofType:@"jpg"]];

            [self.thumbsList addObject:[[NSBundle mainBundle] pathForResource:@"030_thumb" ofType:@"jpg"]];

            [self.thumbsList addObject:[[NSBundle mainBundle] pathForResource:@"031_thumb" ofType:@"jpg"]];

            [self.thumbsList addObject:[[NSBundle mainBundle] pathForResource:@"032_thumb" ofType:@"jpg"]];

            [self.thumbsList addObject:[[NSBundle mainBundle] pathForResource:@"033_thumb" ofType:@"jpg"]];

            [self.thumbsList addObject:[[NSBundle mainBundle] pathForResource:@"034_thumb" ofType:@"jpg"]];

            [self.thumbsList addObject:[[NSBundle mainBundle] pathForResource:@"035_thumb" ofType:@"jpg"]];\

            [self.thumbsList addObject:[[NSBundle mainBundle] pathForResource:@"036_thumb" ofType:@"jpg"]];

            [self.thumbsList addObject:[[NSBundle mainBundle] pathForResource:@"037_thumb" ofType:@"jpg"]];

            [self.thumbsList addObject:[[NSBundle mainBundle] pathForResource:@"038_thumb" ofType:@"jpg"]];

            [self.thumbsList addObject:[[NSBundle mainBundle] pathForResource:@"039_thumb" ofType:@"jpg"]];

            [self.thumbsList addObject:[[NSBundle mainBundle] pathForResource:@"040_thumb" ofType:@"jpg"]];

            [self.thumbsList addObject:[[NSBundle mainBundle] pathForResource:@"041_thumb" ofType:@"jpg"]];

            [self.thumbsList addObject:[[NSBundle mainBundle] pathForResource:@"042_thumb" ofType:@"jpg"]];

            [self.thumbsList addObject:[[NSBundle mainBundle] pathForResource:@"043_thumb" ofType:@"jpg"]];

            [self.thumbsList addObject:[[NSBundle mainBundle] pathForResource:@"044_thumb" ofType:@"jpg"]];

            [self.thumbsList addObject:[[NSBundle mainBundle] pathForResource:@"045_thumb" ofType:@"jpg"]];

            [self.thumbsList addObject:[[NSBundle mainBundle] pathForResource:@"046_thumb" ofType:@"jpg"]];

            [self.thumbsList addObject:[[NSBundle mainBundle] pathForResource:@"047_thumb" ofType:@"jpg"]];

            [self.thumbsList addObject:[[NSBundle mainBundle] pathForResource:@"048_thumb" ofType:@"jpg"]];

            [self.thumbsList addObject:[[NSBundle mainBundle] pathForResource:@"049_thumb" ofType:@"jpg"]];

            [self.thumbsList addObject:[[NSBundle mainBundle] pathForResource:@"050_thumb" ofType:@"jpg"]];

            [self.thumbsList addObject:[[NSBundle mainBundle] pathForResource:@"051_thumb" ofType:@"jpg"]];

            [self.thumbsList addObject:[[NSBundle mainBundle] pathForResource:@"052_thumb" ofType:@"jpg"]];

            [self.thumbsList addObject:[[NSBundle mainBundle] pathForResource:@"053_thumb" ofType:@"jpg"]];

            [self.thumbsList addObject:[[NSBundle mainBundle] pathForResource:@"054_thumb" ofType:@"jpg"]];

            [self.thumbsList addObject:[[NSBundle mainBundle] pathForResource:@"055_thumb" ofType:@"jpg"]];

            [self.thumbsList addObject:[[NSBundle mainBundle] pathForResource:@"056_thumb" ofType:@"jpg"]];

            [self.thumbsList addObject:[[NSBundle mainBundle] pathForResource:@"057_thumb" ofType:@"jpg"]];

            [self.thumbsList addObject:[[NSBundle mainBundle] pathForResource:@"058_thumb" ofType:@"jpg"]];

            [self.thumbsList addObject:[[NSBundle mainBundle] pathForResource:@"059_thumb" ofType:@"jpg"]];

            kNumberOfPages = [galeria count];

            // Comeca a contagem de thumbs de acordo com a array principal

            NSInteger thumbs = 0;

            while (thumbs < kNumberOfPages) {

                float e = 150*thumbs;
                thumbs++;

                // Cria o botao e a view

                UIView *thumbItemView = [[UIView alloc] init];
                UIButton *mathumb = [UIButton buttonWithType:UIButtonTypeCustom];

                NSString *mapURL = [[NSString alloc] initWithFormat:@"%@", [self.thumbsList objectAtIndex:thumbs-1]];

                NSData* imageData = [[NSData alloc]initWithContentsOfFile:mapURL];

                //NSData* imageData = [[NSData alloc]initWithContentsOfURL:[NSURL URLWithString:mapURL]];


                UIImage* image = [[UIImage alloc] initWithData:imageData];
                [mathumb setImage:image forState:UIControlStateNormal];


                // Seta a view da thumb
                thumbItemView.frame = CGRectMake(e, 0, 122, 122);

                UIColor *bkgColor = [[UIColor alloc] initWithPatternImage:[UIImage imageNamed:@"ThumbItemBackground.png"]];
                thumbItemView.backgroundColor = bkgColor;
                thumbItemView.opaque = NO;

                // Seta o tamanho do botao e insere ele na view
                mathumb.frame = CGRectMake(10, 10, 100, 100);
                [thumbItemView addSubview:mathumb];

                // Adiciona o resultado final das thumbs

      开发者_如何学Go          [thumbScroll addSubview:thumbItemView];

                // Tagea cada botao
                mathumb.tag = thumbs;

                // Adiciona a funcao neles de pegar a pagina
                [mathumb addTarget:self action:@selector(goToPage:) forControlEvents:(UIControlEvents)UIControlEventTouchDown];

                [thumbItemView release];

            }

            float s = thumbs*148;

            thumbScroll.contentSize = CGSizeMake(s, 50);
            thumbScroll.showsVerticalScrollIndicator = NO;
            thumbScroll.scrollsToTop = NO;

            //

            // view controllers are created lazily
            // in the meantime, load the array with placeholders which will be replaced on demand
            NSMutableArray *controllers = [[NSMutableArray alloc] init];
            for (unsigned i = 0; i < kNumberOfPages; i++) {
                [controllers addObject:[NSNull null]];
            }
            self.viewControllers = controllers;
            [controllers release];

            // a page is the width of the scroll view
            scrollView.pagingEnabled = YES;
            scrollView.contentSize = CGSizeMake(scrollView.frame.size.width * kNumberOfPages, scrollView.frame.size.height);
            scrollView.showsHorizontalScrollIndicator = NO;
            scrollView.showsVerticalScrollIndicator = NO;
            scrollView.scrollsToTop = NO;
            scrollView.delegate = self;

            pageControl.numberOfPages = kNumberOfPages;
            pageControl.currentPage = 0;

            NSString *qualPagina = [[NSString alloc] initWithFormat:@"Página %i de %i", pageControl.currentPage + 1, kNumberOfPages];
            [curentPageLabel setText:qualPagina];


            // Carrega as paginas

            [self loadScrollViewWithPage:0];
            [self loadScrollViewWithPage:1];

        }

        - (void)loadScrollViewWithPage:(int)page {
            if (page < 0) return;
            if (page >= kNumberOfPages) return;

            // replace the placeholder if necessary
            PageViewController *controller = [viewControllers objectAtIndex:page];
            if ((NSNull *)controller == [NSNull null]) {
                controller = [[PageViewController alloc] initWithPageNumber:page];
                [viewControllers replaceObjectAtIndex:page withObject:controller];

                // Seta a array de cada controller com os enderecos desta.

                controller.enderecos = self.galeria;

                [controller release];
            }

            // add the controller's view to the scroll view
            if (nil == controller.view.superview) {
                CGRect frame = scrollView.frame;
                frame.origin.x = frame.size.width * page;
                frame.origin.y = 0;
                controller.view.frame = frame;
                [scrollView addSubview:controller.view];
            }
        }

        - (void)scrollViewDidScroll:(UIScrollView *)sender {
            // We don't want a "feedback loop" between the UIPageControl and the scroll delegate in
            // which a scroll event generated from the user hitting the page control triggers updates from
            // the delegate method. We use a boolean to disable the delegate logic when the page control is used.
            if (pageControlUsed) {
                // do nothing - the scroll was initiated from the page control, not the user dragging
                return;

            }

            // Switch the indicator when more than 50% of the previous/next page is visible
            CGFloat pageWidth = scrollView.frame.size.width;
            int page = floor((scrollView.contentOffset.x - pageWidth / 2) / pageWidth) + 1;
            pageControl.currentPage = page;


            // Arruma as paginas

            NSString *qlPage = [[NSString alloc] initWithFormat:@"Página %i de %i", page+1, kNumberOfPages];
            curentPageLabel.text = qlPage;
            //NSLog(@"QL Q TA %@", qlAtual.text);

            // load the visible page and the page on either side of it (to avoid flashes when the user starts scrolling)
            [self loadScrollViewWithPage:page - 1];
            [self loadScrollViewWithPage:page];
            [self loadScrollViewWithPage:page + 1];

            // A possible optimization would be to unload the views+controllers which are no longer visible

        }

        -  (void)goToPage:(id)sender {


            UIButton* btn = (UIButton *) sender;
            NSLog(@"Deu ein!, o sender eh %i", btn.tag);

            float t = btn.tag-1;
            float c = 768*t;

            scrollView.contentOffset = CGPointMake(c, 0);

            NSString *qualPagina = [[NSString alloc] initWithFormat:@"Página %i de %i", btn.tag, kNumberOfPages];
            [curentPageLabel setText:qualPagina];

        }

        // At the begin of scroll dragging, reset the boolean used when scrolls originate from the UIPageControl
        - (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView {
            pageControlUsed = NO;
            [self closePanel];

        }

        // At the end of scroll animation, reset the boolean used when scrolls originate from the UIPageControl
        - (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView {
            pageControlUsed = NO;

            //NSString *qlPage = [[NSString alloc] initWithFormat:@"%@ de %i", self.qlAtual.text, kNumberOfPages];

            //currentPageLabel.text = qlPage;

            //[qlPage release];

        }

        - (IBAction)changePage:(id)sender {
            int page = pageControl.currentPage;

            // load the visible page and the page on either side of it (to avoid flashes when the user starts scrolling)
            [self loadScrollViewWithPage:page - 1];
            [self loadScrollViewWithPage:page];
            [self loadScrollViewWithPage:page + 1];

            // update the scroll view to the appropriate page
            CGRect frame = scrollView.frame;
            frame.origin.x = frame.size.width * page;
            frame.origin.y = 0;
            [scrollView scrollRectToVisible:frame animated:YES];

            // Set the boolean used when scrolls originate from the UIPageControl. See scrollViewDidScroll: above.
            pageControlUsed = YES;

        }

THanks!


First of all only load images that you need to show. Load the rest of them only when they are needed. As soon as images dissapear release them from memory.

Also it seems that you forgot to release many variables i.e. mapURL, imageData, image bkgColor.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜