views:

1104

answers:

3

Hey guys first I got a picture into a image view that was 2900x2100 and the file size was about 4MB I also had an scroll view to move around the image. Anyways it crashed, first I though it was because the 4MB size. Then I resized it making it 1287x1234 and 1.3MB but it was pixelated when I zoom it too much. After that I manage to make it 2900x2100 with 1.5 MB! so I ran it on my device and it still crashes!!

Anybody knows why's that? This is what I get on the debugger console

Program received signal: “0”.

warning: check_safe_call: could not restore current frame

here's more info, this is my viewDidLoad method:

UIImageView *tempimage = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"sanlucasFLAT.jpg"]];

self._FLatLucasImageView = tempimage;

[tempimage release];

_FLatLucasImageView.frame = CGRectMake(-7, 0, 800, 800);

_FLatLucasImageView.contentMode = UIViewContentModeScaleAspectFit;

_FlatLucasScrollView.contentSize = CGSizeMake(_FLatLucasImageView.frame.size.width,_FLatLucasImageView.frame.size.height);

_FlatLucasScrollView.delegate = self;

_FlatLucasScrollView.clipsToBounds = YES;

[_FlatLucasScrollView addSubview:_FLatLucasImageView];

[_FLatLucasImageView release];

When I set the CGRectMake to: (0,0,2900,2961) it doesn't crash when I scroll the image

Best Regards

Carlos Vargas

A: 

This code is very messy. Couple of things:

  • Try not to use instance variables with an underscore prefix (apple semi-private)
  • Try not to use instance variables start with a uppercase letter (convention, things start start with an uppercase letter are usually classes or types)
  • Do not define properties for instance variables that you do not actually expose. If they are simply things internal to your class then you do not need a property.

That being said, my guess is that you are over-releasing the image view. Try removing that last [_FLatLucasImageView release].

If you do not need to access that image view after you have added it to the scroll view then I suggest to completely remove the instance variable and property and simply have code like this:

UIImage* image = [UIImage imageNamed: @"sanlucasFLAT.jpg"];
if (image != nil)
{
    UIImageView* imageView = [[[UIImageView alloc] initWithImage: image] autorelease];
    if (imageView != nil)
    {
        imageView.frame = CGRectMake(-7, 0, 800, 800);
        imageView.contentMode = UIViewContentModeScaleAspectFit;

        flatLucasScrollView.contentSize = CGSizeMake(imageView.frame.size.width, imageView.frame.size.height);
        flatLucasScrollView.delegate = self;
        flatLucasScrollView.clipsToBounds = YES;

        [flatLucasScrollView addSubview: imageView];
    }
}
St3fan
+1  A: 

The reason why your app crashed is because the picture you are using is too big.
According to the class reference of UIImage, it says that You should avoid creating UIImage objects that are greater than 1024 x 1024 in size.

If you add a UIImage object which has a bigger image than 1024 x 1024 to a UIImageView object, your app could crush. If you are lucky, sometime your app doesn't crush thought.

tomute
+2  A: 

The file size of your image doesn't really matter here: UIImage will have to decompress it and store the decompressed image in memory. It would usually need 4 bytes for each pixel, which means a 2900 x 2100 pixel image will take up more than 24 MB of memory. On an iPhone or iPhone 3G you might not even have that much memory available for your whole program.

Maybe try splitting it up into smaller images and then load them as necessary when the user scrolls around your view. Just cut it into tiles that are about 320x480 or so (the iPhone's screen size) and then only add the visible tiles to a UIScrollView, and remove them when the user scrolls away and they become invisible.

Thomas Müller