Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

received memory warning then crash

I want to display multiple images in scrollview when images are more than 70 application will crash and display received memory error.I have get the images from document Directory

I have tried -

 UIScrollView *MyScroll=[[UIScrollView alloc]initWithFrame:CGRectMake(0, 0, 320, 480)];

    int x=5,y=15;

    for (int i = 0; i < imgarr.count; i++)
    {
        if(x<=211)
        {

        UIImage* imagePath = [UIImage imageWithContentsOfFile:[path_array objectAtIndex:i]];

        imgView=[[UIImageView alloc]initWithFrame:CGRectMake(x, y, 77, 75)];
        imgView.image=imagePath;

            imgeBtn=[UIButton buttonWithType:UIButtonTypeCustom];
            imgeBtn.frame=CGRectMake(x, y, 93, 110);
            [imgeBtn addTarget:self action:@selector(btnclick:) forControlEvents:UIControlEventTouchUpInside];
            imgeBtn.tag=i;
            x+=103;

        }
        else
        {
            x=5;
            y+=130;


        UIImage* imagePath = [UIImage imageWithContentsOfFile:[path_array objectAtIndex:i]];

        imgView=[[UIImageView alloc]initWithFrame:CGRectMake(x, y, 77, 75)];
        imgView.image=imagePath;

            imgeBtn=[UIButton buttonWithType:UIButtonTypeCustom];
            imgeBtn.frame=CGRectMake(x, y, 93, 110);
            [imgeBtn addTarget:self action:@selector(btnclick:) forControlEvents:UIControlEventTouchUpInside];
            imgeBtn.tag=i;
            x+=103;

        }
        [MyScroll addSubview:imgeBtn];
        [MyScroll addSubview:imgView];
         MyScroll.contentSize=CGSizeMake(320, y+120);

How can I display multiple images in scrollview?

like image 429
sohil Avatar asked May 26 '15 11:05

sohil


People also ask

Does memory related issue can cause application crash?

Your application can be terminated because other applications are consuming too much memory, and if your application is doing any work at the time (backup, server-sync, cache pruning, etc.) you may end up with a crash report that doesn't appear to be memory related.

How to handle memory warning in iOS?

Failure to reduce your app's memory usage may result in your app's termination. Therefore, consider writing any unsaved data to disk as part of your cleanup efforts. To test your app's response to a low-memory warning, use the Simulate Memory Warning command in iOS Simulator.

What is didReceiveMemoryWarning?

When didReceiveMemoryWarning is called, it means your app is using too much memory (compare with memory of device), and you should release any additional memory used by your view controller to reduce the memory of your app. If the memory app gets over the memory of device, iOS will kill your app immediately.

How much memory can iOS app use?

Beta versions of iOS 15 and iPadOS 15 now give developers the option of requesting more RAM than the current 5GB maximum per app, with limitations. Apple has always set a cap on how much RAM any one app can use on the iPad, but it's become more of an issue as the devices themselves physically include more.


2 Answers

The advice others have given to switch to a UICollectionView is good, but it has missed the real flaw in your code: the method imageNamed: of UIImage is keeping all 70 of your images in memory forever. Per the docs:

If you have an image file that will only be displayed once and wish to ensure that it does not get added to the system’s cache, you should instead create your image using imageWithContentsOfFile:. This will keep your single-use image out of the system image cache, potentially improving the memory use characteristics of your app.

Switching to this method will prevent the crashes, if you only load into memory the images that you need. UICollectionView makes this trivial, since you can only load image when the collection view asks you to fill out a cell object in its datasource's collectionView:cellForItemAtIndexpath: method.

like image 50
Ben Pious Avatar answered Sep 30 '22 16:09

Ben Pious


Create custom class of collection view cell 
and You can pass image name in array. 

#import <UIKit/UIKit.h>

@interface ViewController : UIViewController<UICollectionViewDataSource,UICollectionViewDelegate>

@property (weak, nonatomic) IBOutlet UICollectionView *Collection;

@end

#import "ViewController.h"
#import "CustomCell.h"
@interface ViewController ()
@end
@implementation ViewController
- (void)viewDidLoad
{
    [super viewDidLoad];
    [self.Collection registerClass:[CustomCell class] forCellWithReuseIdentifier:@"cell"];
    [self.Collection registerNib:[UINib nibWithNibName:@"CustomCell" bundle:nil] forCellWithReuseIdentifier:@"cell"];
}

-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return 5;
}
-(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
    return 4;
}

-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    CustomCell * cell=[collectionView dequeueReusableCellWithReuseIdentifier:@"cell" forIndexPath:indexPath];
    cell.imgview.image=[UIImage imageNamed:@"XYZ.png"];
    return cell;
}
like image 38
Dnyaneshwar Wakchaure Avatar answered Sep 30 '22 15:09

Dnyaneshwar Wakchaure