开发者

UIImage imageWithData

开发者 https://www.devze.com 2023-01-26 16:47 出处:网络
I have a UITable and when I click on a row I load a set of images. I create the images with UIImage *image = [UIImage imageWithData:data];

I have a UITable and when I click on a row I load a set of images. I create the images with

UIImage *image = [UIImage imageWithData:data];

The problem is that ever time I click on the same row, the images are re-allocated into memory, like if no cache it is used and no memory is freed, so the allocated memory keep increasing. (I see this using the Inspector on both the device and the emulator)

I am releasing everything and also, with clang, everything looks fine. The point is that after a while my app crashes.

Any suggestion or idea?

EDIT:

Here is the portion of code I think is generating the issue:

UIView *main = [[[UIView alloc] initWithFrame:rectScrollView] autorelease];

    for (int i=0; i<[contentArray count]; i++) {
        float targetWidth = rectScrollView.size.width;
        float targetHeight = rectScrollView.size.height;

        Link *tmp_link = [contentArray objectAtIndex:i];
        AsyncImageView* asyncImage = nil;
        if(tmp_link!=nil){
            CGRect imageFrame = CGRectMake(targetWidth*i, 0, targetWidth, targetHeight); 
            //asyncImage = [[[AsyncImageView alloc] initWithFrame:imageFrame ] autorelease];
            asyncImage = [[AsyncImageView alloc] initWithFrame:imageFrame ];
            asyncImage.contentMode = UIViewContentModeScaleAspectFit;
            NSURL* url = [NSURL URLWithString:[[IMG_SERVER_URL stringByAppendingString: tmp_link.url] stringByAddingPercentEscapesUsingEncoding: NSUTF8StringEncoding]];
            [asyncImage loadImageFromURL:url];
            [self.scrollView addSubview:(UIImageView *)asyncImage];
            [asyncImage release];
        }

        if ([tmp_link.caption length] > 0 && asyncImage!=nil) {
            float marginTop = 25;

            UILabel *caption = [[UILabel alloc] init];
            caption.text = tmp_link.caption;
            caption.textAlignment = UITextAlignmentCenter;
            caption.textColor = [UIColor whiteColor];
            caption.font = [UIFont systemFontOfSize:12];
            caption.backgroundColor = [UIColor colorWithWhite:0.0 alpha:0.4];

            CGRect captionFrame = CGRectMake((i * targetWidth), marginTop+10, targetWidth, 20.0f);
            caption.frame = captionFrame;   
            [self.scrollView addSubview:caption];
            [caption release];
        }
        //[asyncImage release];
    }
    [main addSubview:scrollView];
    [scrollView release];
    if (pageControlEnabledTop) {
        rectPageControl = CGRectMake(0, 5, scrollWidth, 15);
    }
    else if (pageControlEnabledBottom) {
        //rectPageControl = CGRectMake(0, (scrollHeight - 25), scrollWidth, 15);
        rectPageControl = CGRectMake(0, (scrollHeight), scrollWidth, 15);
    }
    if (pageControlEnabledTop || pageControlEnabledBottom) {
        //pageControl = [[[UIPageControl alloc] initWithFrame:rectPageControl] autorelease];
        pageControl = [[UIPageControl alloc] initWithFrame:rectPageControl];
        pageContro开发者_高级运维l.numberOfPages = [contentArray count];
        pageControl.currentPage = page;
        [main addSubview:pageControl];
        [pageControl release];
    }
    if (pageControlEnabledTop || pageControlEnabledBottom || rememberPosition) self.scrollView.delegate = self;
    //if (margin) [margin release];
    return (UIScrollView *)main;

And it is used like this:

reviewImagesImage = [[IGUIScrollViewImage alloc] init];
        if([currentReview.linkScreenshots count]>0){

            //reviewImages
            reviewImages = [[UIView alloc] init];

            //placing the images
            CGRect frameImages = reviewImages.frame;
            frameImages.origin.x = 10;
            frameImages.origin.y = (infoView.frame.origin.y + infoView.frame.size.height + 10.0f);
            frameImages.size.width = 300;
            frameImages.size.height = 350.0f;
            reviewImages.frame = frameImages;
            reviewImages.backgroundColor =  [UIColor clearColor];
            [reviewImagesImage setContentArray:currentReview.linkScreenshots];
            [reviewImagesImage setWidth:reviewImages.frame.size.width andHeight: reviewImages.frame.size.height];
            [reviewImagesImage enablePageControlOnBottom];
            [reviewImagesImage setBackGroudColor: [UIColor clearColor]];
            UIScrollView *tmp_sv = [reviewImagesImage getWithPosition:0];
            [reviewImages addSubview:tmp_sv];
            [self.view addSubview:reviewImages];
                    [reviewImages release];
        }

I have noticed that if I try to release reviewImagesImage my app crashes :/


Of course no cache is used because new UIImage will be created from the data every time. If memory usage significantly increased like you have said, it is most likely that it is our fault that unintentionally keep the reference retained somewhere.


Once you've set your image with imageWithData, save the image to the filepath in the app bundle.

0

精彩评论

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