UIImage图像与数据

发布于 2024-10-03 17:24:25 字数 4328 浏览 1 评论 0原文

我有一个 UITable,当我单击一行时,我会加载一组图像。 我使用以下命令创建图像

UIImage *image = [UIImage imageWithData:data];

问题是,每当我单击同一行时,图像都会重新分配到内存中,就像没有使用缓存并且没有释放内存一样,因此分配的内存不断增加。 (我在设备和模拟器上使用检查器看到这一点)

我正在释放所有内容,而且,随着 clang,一切看起来都很好。 关键是一段时间后我的应用程序崩溃了。

有什么建议或想法吗?

编辑:

这是我认为产生问题的代码部分:

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];
        pageControl.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;

它的使用方式如下:

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];
        }

我注意到,如果我尝试发布 reviewImagesImage 我的应用程序崩溃:/

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];
        pageControl.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 :/

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

向地狱狂奔 2024-10-10 17:24:25

当然不使用缓存,因为每次都会从数据创建新的 UIImage。如果内存使用量像您所说的那样显着增加,很可能是我们无意中将引用保留在某处的错误。

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.

无所谓啦 2024-10-10 17:24:25

使用 imageWithData 设置图像后,将图像保存到应用程序包中的文件路径

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文