在iPad应用程序中清理内存

时间:2014-02-25 19:05:47

标签: ios ipad memory

当用户关闭标签时,如何在iPad应用中清理内存?例如:用户在一个选项卡中看到图像滑块,转到另一个选项卡,此时图像滑块必须从内存中卸载并在打开滑块的选项卡时再次加载。你能帮帮我吗?这是我的源代码:

.h

@interface ViewController : UIViewController <UIScrollViewDelegate>

@end

@interface PhotoViewController : UIViewController

@property (nonatomic, strong) IBOutlet UIScrollView *scrollView;
@property (nonatomic, strong) IBOutlet UIPageControl *pageControl;
@property (nonatomic, strong) NSArray *imageArray;

@end

.m

#import "PhotoViewController.h"

@interface PhotoViewController ()

@end

@implementation PhotoViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
    //Put the names of our image files in our array.
    imageArray = [[NSArray alloc] initWithObjects:@"ph1.jpg", @"ph2.jpg", @"ph3.jpg",@"ph4.jpg",@"ph5.jpg",@"ph6.jpg",@"ph7.jpg",@"ph8.jpg",@"ph9.jpg",@"ph10.jpg",@"ph11.jpg",@"ph12.jpg",@"ph13.jpg",@"ph14.jpg",@"ph15.jpg",@"ph16.jpg",@"ph17.jpg",@"ph18.jpg",@"ph19.jpg",@"ph20.jpg",@"ph21.jpg",@"ph22.jpg",@"ph23.jpg",@"ph24.jpg", nil];

    for (int i = 0; i < [imageArray count]; i++) {
        //We'll create an imageView object in every 'page' of our scrollView.
        CGRect frame;
        frame.origin.x = self.scrollView.frame.size.width * i;
        frame.origin.y = 0;
        frame.size = self.scrollView.frame.size;

        UIImageView *imageView = [[UIImageView alloc] initWithFrame:frame];
        imageView.image = [UIImage imageNamed:[imageArray objectAtIndex:i]];
        [self.scrollView addSubview:imageView];
    }
    //Set the content size of our scrollview according to the total width of our imageView objects.
    scrollView.contentSize = CGSizeMake(scrollView.frame.size.width * [imageArray count], scrollView.frame.size.height);
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - UIScrollView Delegate
- (void)scrollViewDidScroll:(UIScrollView *)sender
{
    // Update the page when more than 50% of the previous/next page is visible
    CGFloat pageWidth = self.scrollView.frame.size.width;
    int page = floor((self.scrollView.contentOffset.x - pageWidth / 2) / pageWidth) + 1;
    self.pageControl.currentPage = page;
}

@synthesize scrollView;
@synthesize pageControl;
@synthesize imageArray;

@end

0 个答案:

没有答案