Advertise Mobile SDKs Books Events Forum News Social Networking Support Us
Follow @iphonedevsdk on Twitter

Interface 2, Advanced iOS
Mockup & Code Gen
($9.99)

Make your own iPhone apps
and run them live!
(free)

Pic Frame Dynamo: Photo Editing
($0.99)

Abiliator
($1.99)

Want your application or service advertised on iPhone Dev SDK?

Go Back   iPhone Dev SDK Forum

View Single Post
Old 12-29-2009, 10:36 AM   #1 (permalink)
leemmccormick
Registered Member
 
Join Date: Oct 2009
Posts: 2
leemmccormick is on a distinguished road
Unhappy Several UIImageViews in UIScrollView (photo app) crashes

I have a problem with a photo viewing application I've written.

I have a UITabBarController with a UINavigationController inside. The UINavigationController initially displays a UITableView. On selection it pushes another UIViewController (Individual photo album) along with an NSArray of photos. This Controller contains a UIScrollView/UIPageControl which displays several UIViewControllers in which there is a UIImageView.

The application initially works great. It loads each image correctly for each Album and you can go back from the navigation bar. The problem is after about 180 images the app starts to throw memory warnings and eventually crashes throwing "Program received signal: “0”. warning: check_safe_call: could not restore current frame" which I believe is to do with low memory. It's incredibly frustrating because I've checked and there no leaks (according to leaks intrument) and every dealloc is being called as it should if I add NSLogs in the relevant places. The dealloc methods release every retained property and set them to nil.

If you check Alloc instruments it shows the memory usage gradually going up after every album has been viewed. It does release some memory but not all of it. e.g. if the album uses 1MB to display 0.9MB might be released. In Memory Monitor is shows hardly any memory being released at all.

Why can't I get what should be so simple to work? Any help to a solution would be appreciated as I've hit a wall. This is the last issue before I can release it and I need to do it asap!

This is a link to the basic project files which has everything I've written. If the code posted below isn't enough this should make more sense. Rock Photography v2.zip


AlbumsViewController (pushes an individual "albumviewcontroller")

Code:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {

 NSMutableDictionary *dictThisItem = [self.arrAlbums objectAtIndex:[indexPath row]];
    NSString *strBand = [dictThisItem objectForKey:@"album"];

  NSMutableArray *arrThesePhotos = [[self.arrAlbums objectAtIndex:[indexPath row]] objectForKey:@"photos"];

  if (self.albumViewController == nil){
   self.albumViewController = [[AlbumViewController alloc] initWithNibName:nil bundle:nil];
  }
  albumViewController.hidesBottomBarWhenPushed = YES;
  [self.navigationController pushViewController:albumViewController animated:YES];
  self.albumViewController.arrPhotos = arrThesePhotos;
  [albumViewController populateScroller];
}
AlbumViewController

Code:
- (void)populateScroller {

imagesScroller.pagingEnabled = YES;
imagesScroller.contentSize = CGSizeMake(imagesScroller.frame.size.width * [self.arrPhotos count], 380);
imagesScroller.showsHorizontalScrollIndicator = NO;
imagesScroller.showsVerticalScrollIndicator = NO;
imagesScroller.scrollsToTop = NO;
imagesScroller.delegate = self;
imagesScroller.backgroundColor = [UIColor blackColor];
[imagesScroller scrollRectToVisible:CGRectMake(0.0, 0.0, 320.0, 480.0) animated:NO];

pageControl.numberOfPages = [self.arrPhotos count];
pageControl.currentPage = 0;
 pageControl.backgroundColor = [UIColor blackColor];



 NSMutableArray *controllers = [[NSMutableArray alloc] init];
for (int i = 0; i < [self.arrPhotos count]; i++) {

  CGRect frame = imagesScroller.frame;
    frame.origin.x = frame.size.width * i;
    frame.origin.y = 0;

  NSString *strImagePath = [[self.arrPhotos objectAtIndex:i] stringByReplacingOccurrencesOfString:@"iPhone" withString:@"iPhone_thumbnail"];

  ImageViewController *imageViewController = [ImageViewController alloc];
  imageViewController.localImage = YES;
  imageViewController.albumViewController = self;
  [imageViewController initWithPhotoName:strImagePath];
  [controllers addObject:imageViewController];

  imageViewController.view.frame = frame;
  [imagesScroller addSubview:imageViewController.view];

  [imageViewController release];

}
self.viewControllers = controllers;
[controllers release];

}
ImageViewController

Code:
- (void)viewDidLoad {

 self.navigationShown = NO;

 Cache *cache = [[Cache alloc] init];
 [cache release];


 NSString *strURL = [@"http://www.site.co.uk" stringByAppendingString:self.strThisPhoto];
 NSString *strTmpPrefix = (self.localImage) ? @"_tmp_rockphotothumb_" : @"_tmp_rockphotolarge_";

// Cache Paths
NSArray *arrPaths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
NSString *strLocalPath = [[arrPaths objectAtIndex:0] stringByAppendingString:@"/"];
NSString *strPrefix = (strTmpPrefix != nil) ? strTmpPrefix : @"_tmp_rockphotolarge_";

NSMutableArray *arrImagePaths = (NSMutableArray *)[strURL componentsSeparatedByString:@"/"];


// Check cache
NSString *strEntireLocalCache = [strLocalPath stringByAppendingString:[strPrefix stringByAppendingString:[arrImagePaths objectAtIndex:[arrImagePaths count]-1]]];
if ([[NSFileManager defaultManager] fileExistsAtPath:strEntireLocalCache]){

    UIImageView *imvImageView = [UIImageView alloc];
    UIImage *image = [[UIImage imageWithContentsOfFile:strEntireLocalCache] autorelease]; 
    [imvImageView initWithImage:image];

    CGSize imgSize = image.size;
    CGFloat fltWidth = imgSize.width;
    CGFloat fltHeight = imgSize.height;

    // If landscape rotate image
    if (fltWidth > fltHeight){
        imvImageView.frame = CGRectMake(-80.0, 80.0, 481.0, 320.0);

        CGAffineTransform rotate = CGAffineTransformMakeRotation(-1.57079633);
        [imvImageView setTransform:rotate];
    }else{
        imvImageView.frame = CGRectMake(0.0, 0.0, 320.0, 481.0);
    }

    [self.view addSubview:imvImageView];
    [imvImageView release];

}else{

    // Data URL Downloading
    [[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:YES];
    NSData *datImageData = [NSData dataWithContentsOfURL: [NSURL URLWithString:strURL]];
    [[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:NO];

    [datImageData writeToFile:strEntireLocalCache atomically:YES];

    UIImageView *imvImageView = [UIImageView alloc];
    UIImage *image = [[UIImage imageWithData: datImageData] autorelease];
    [imvImageView initWithImage:image];

    CGSize imgSize = image.size;
    CGFloat fltWidth = imgSize.width;
    CGFloat fltHeight = imgSize.height;

    // If landscape rotate image
    if (fltWidth > fltHeight){
        imvImageView.frame = CGRectMake(-80.0, 80.0, 481.0, 320.0);

        CGAffineTransform rotate = CGAffineTransformMakeRotation(-1.57079633);
        [imvImageView setTransform:rotate];
    }else{
        imvImageView.frame = CGRectMake(0.0, 0.0, 320.0, 481.0);
    }

    [self.view addSubview:imvImageView];
    [imvImageView release];

}

}
leemmccormick is offline   Reply With Quote
 

» Advertisements
» Online Users: 649
17 members and 632 guests
ADY, BdR, Droverson, Duncan C, esoteric, givensur, jimmymacapps, krisk, mraalex, RZApplications, sacha1996, SLIC, smithdale87, smithms, TheStalker, Touchmint, Yoshkow
Most users ever online was 1,387, 04-10-2012 at 04:21 AM.
» Stats
Members: 175,422
Threads: 94,019
Posts: 402,546
Top Poster: BrianSlick (7,978)
Welcome to our newest member, iAppDeveloper
Powered by vBadvanced CMPS v3.1.0

All times are GMT -5. The time now is 02:26 PM.
Powered by vBulletin® Version 3.8.0
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.