Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8210351
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T10:02:50+00:00 2026-06-07T10:02:50+00:00

I am reading images from document directory in a loop. for(iArrCount=0;iArrCount<[arrImages count];iArrCount++) { UIButton

  • 0

I am reading images from document directory in a loop.

for(iArrCount=0;iArrCount<[arrImages count];iArrCount++)
    {
UIButton *btnImage = [UIButton buttonWithType:UIButtonTypeCustom];
            btnImage.frame = CGRectMake(xRow, yRow, width, height);
            [btnImage addTarget:self action:@selector(imageDetails:) forControlEvents:UIControlEventTouchUpInside];
            btnImage.tag = iCount;
            if(iCount<[arrImages count])
            {


                NSString *workSpacePath=[[self applicationDocumentsDirectory] stringByAppendingPathComponent:[arrImages objectAtIndex:iCount]];
                [btnImage setBackgroundImage:[UIImage imageWithData:[NSData dataWithContentsOfFile:workSpacePath]] forState:UIControlStateNormal];
                [scrollImages addSubview:btnImage];
}
}  

On doing so, control goes to didRecieveMemoryWarning and application crashes. If I replace the image by a image from resource folder, application does not crashes. Why so?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-07T10:02:52+00:00Added an answer on June 7, 2026 at 10:02 am

    The problem is that your buttons are maintaining a reference to the full resolution image. What you will need to do is scale down the image to your button dimensions and set that scaled down image as your button background. Something like this should do the trick:

    Create a category on UIImage… Let’s call it UIImage+Scaler.h

    // UIImage+Scaler.h
    #import <UIKit/UIKit.h>
    
    @interface UIImage (Scaler)
    - (UIImage*)scaleToSize:(CGSize)size;
    @end
    

    And the implementation:

    // UIImage+Scaler.m
    #import "UIImage+Scaler.h"
    
    #define kBitsPerComponent 8
    #define kBitmapInfo       kCGImageAlphaPremultipliedLast
    
    - (UIImage*)scaleToSize:(CGSize)size
    {
        CGBitmapInfo bitmapInfo = kBitmapInfo;
        size_t bytesPerRow = size.width * 4.0;
        CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
        CGContextRef context = CGBitmapContextCreate(NULL, size.width, 
                                       size.height, kBitsPerComponent, 
                                       bytesPerRow, colorSpace, bitmapInfo);
    
        CGRect rect = CGRectMake(0.0f, 0.0f, size.width, size.height);
        CGContextDrawImage(context, rect, self.CGImage);
    
        CGImageRef scaledImageRef = CGBitmapContextCreateImage(context);
        UIImage* scaledImage = [UIImage imageWithCGImage:scaledImageRef];
    
        CGImageRelease(scaledImageRef);
        CGContextRelease(context);
        CGColorSpaceRelease(colorSpace);
    
        return scaledImage;
    }
    

    OK, now back to your code. Like the other posters said you’ll need an autorelease pool.

    CGSize buttonSize = CGSizeMake(width, height);
    
    for(iArrCount=0;iArrCount<[arrImages count];iArrCount++)
    {
        NSAutoreleasePool* pool = [[NSAutoreleasePool alloc] init];
        UIButton *btnImage = [UIButton buttonWithType:UIButtonTypeCustom];
        btnImage.frame = CGRectMake(xRow, yRow, width, height);
        [btnImage addTarget:self 
                     action:@selector(imageDetails:)      
           forControlEvents:UIControlEventTouchUpInside];
        btnImage.tag = iCount;
        if(iCount<[arrImages count])
        {
            NSString *workSpacePath=[[self applicationDocumentsDirectory]  
                           stringByAppendingPathComponent:
                                [arrImages objectAtIndex:iCount]];
            UIImage* bigImage = [UIImage imageWithData:[NSData   
                                 dataWithContentsOfFile:workSpacePath]];
            UIImage* smallImage = [bigImage scaleToSize:buttonSize];
            [btnImage setBackgroundImage:smallImage forState:UIControlStateNormal];
            [scrollImages addSubview:btnImage];
        }
        [pool drain]; // must drain pool inside loop to release bigImage
    } 
    

    Hope this solves for problem.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am having a servlet which is reading images from database, how can i
Abstract: reading images from file with toggled bits to make unusable for preview tools
In my iPhone app testing environment, I am reading in .png images from a
Possible Duplicate: Reading and writing images to an SQLite DB for iPhone use How
I've been reading up on the best way to handle images for a website
I am reading a DICOMDIR and then displaying all the images of a given
Reading the docs, I'd expect $(#wrap2).remove(.error) to remove all .error elements from #wrap2 .
I'm wondering how Android's implementation of SQLite handles long Strings. Reading from online documentation
I assume that you can open images from your resources folder with Intent.ACTION_VIEW ?
I'm trying to read data from an Excel document in C# using Microsofts COM

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.