0

I have in my application many animations (like a 70, 10 in 7 viewcontrollers) with the same type of voids and functions, and when I run on my device or in the simulator, the application crashes.
How can I improve these animations using threads or dispatch, or the problem is the memory?
How could be done this, in another questions & tutorials, using the same method's for the animations & are very simple, the code is like this and I repeat for any animation that I need in my App:

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view.

    [self CargarSonidos];
    [self getArrayEffectImagen1];
    self.imgImagen1.animationImages = self.arrayEffectImagen1;
    self.imgImagen1.animationDuration = 1.0f;
    [sonido play];

}

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

- (void)getArrayEffectImagen1
{
    NSMutableArray *arrayEfecto = [[NSMutableArray alloc] init];

    for (int i=0; i<14; i++) {
        UIImage *imagen = [UIImage imageNamed:[NSString stringWithFormat:@"Imagen%d.png",i]];
        [arrayEfecto addObject:imagen];
    }

    self.arrayEffectImagen1 = [NSArray arrayWithArray:arrayEfecto];
}

-(void)CargarSonidos
{
    NSURL *Imagen1Url = [NSURL fileURLWithPath:[[NSBundle mainBundle]pathForResource:@"PrimerSonido"ofType:@"MP3"]];

    NSError *error;
    Sonido = [[AVAudioPlayer alloc]initWithContentsOfURL:Imagen1Url error:&error];

    if (error)
    {
        NSLog(@"Error in audioPlayer: %@",[error localizedDescription]);
    }
    else
    {
        Sonido.delegate = self;
        [Sonido prepareToPlay];
    }
}

-(IBAction)Animacion1
{
    [Sonido play];
    [self.imgImagen1 setAnimationRepeatCount:1];
    [self.imgImagen1 startAnimating];
}

-(void)audioPlayerDidFinishPlaying:
(AVAudioPlayer *)player successfully:(BOOL)flag
{
}
-(void)audioPlayerDecodeErrorDidOccur:
(AVAudioPlayer *)player error:(NSError *)error
{
}
-(void)audioPlayerBeginInterruption:(AVAudioPlayer *)player
{
}
-(void)audioPlayerEndInterruption:(AVAudioPlayer *)player
{
}

- (void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:animated];
    [Sonido stop];
}

-(IBAction)TapOne:(UITapGestureRecognizer *)recognizer
{
    [self Animacion1];
}

I'm using Xcode 4.6 & the app is for iOS 5.0. Im using ARC with storyboard & modal or push segue, I did try in a simple way create this app, So if you know something or help me how can I fix or improve this code in a better way with threads or dispatch maybe?

AND o yes, well the Crash LOG is:

Incident Identifier: 3B38AB75-8A16-460E-BC07-55F5A0D4D2E6
CrashReporter Key:   3c4dd8277fec52e8c14d73ab348b62650bbf5226
Hardware Model:      iPhone3,1
OS Version:          iPhone OS 6.1.3 (10B329)
Kernel Version:      Darwin Kernel Version 13.0.0: Wed Feb 13 21:36:52 PST 2013; root:xnu-2107.7.55.2.2~1/RELEASE_ARM_S5L8930X
Date:                2013-06-20 10:18:17 -0400
Time since snapshot: 102 ms

Free pages:        1037
Active pages:      3050
Inactive pages:    2264
Throttled pages:   103507
Purgeable pages:   0
Wired pages:       19150
Largest process:   Simple Juego Prototipo

Processes
     Name                    <UUID>                       rpages       recent_max       [reason]          (state)

           timed <129ae7acc9bc3209a60ac42d49b0d89f>          290              379         [vm]         (daemon) (idle)
       Messenger <ee1787e4954037c4a692b76e4d7d4b6e>         1496             2138         [vm]         (resume)
       MobileSMS <3d75b813ead134998225651ca662356a>         1347             1347         [vm]         (background)
      MobileMail <e07ca7a7280736c7bf301451f89d1c02>          978              978         [vm]         (resume) (continuous)
     MobilePhone <51866c9bc9f93c9d8526e8acf9efc64c>         2181             2181         [vm]         (continuous)
            tccd <2778744d99a530c9a7d5e57a19b60008>          189              189         [vm]         (daemon)
             kbd <f7341c9a161a39acb5bc18ea47d6ebb7>          499              499         [vm]         (daemon)
      librariand <5c2a34477ddf3501aed0ec2d25caa09e>          208              208         [vm]         (daemon)
 Juego Prototipo <63bf4a5a839d3b3fa91700566bff69b3>        88101            88101         [vm]         (audio) (frontmost) (resume)
            ptpd <096297a7a40f318290a972274cc44d87>          735              735                      (daemon)
           wifid <72eda25ccdd737a79d6cd172e9bf99a5>          336              336                      (daemon)
    mediaserverd <af72564bf8713fb8b16ee64c5ee85568>         1004             1004                      (daemon)
     dataaccessd <abc7f74d5d683d4c9a04e7ade5b1126b>         1237             1237                      (daemon)
       locationd <137100f254373daeb894f665c413f8b0>          655              655                      (daemon)
   iaptransportd <9b3cf56b4db13761bbd8ac2e0ff099be>          223              223                      (daemon)
         syslogd <da215e2d3de133bf8f52590727b9bdfe>          146              146                      (daemon)
     SpringBoard <bd1d77d154ef3dffbb53d89337c8a08f>         3299             3299                     
      backboardd <e1dc74434e0e3938b17ff0f7ad85d138>         3944             3944                      (daemon)
         configd <b6a4d70640b63e8f82e0e9f3d6ee2bb9>          449              449                      (daemon)
       lockdownd <a123aa04ddf83a5fae8e5bc08f0b5771>          248              248                      (daemon)
   fairplayd.N90 <178af330594b392eab2c7b4499001e75>          143              143                      (daemon)
          powerd <63673a83ac9c3cf98fdc75df8ba70fd0>          143              143                      (daemon)
  UserEventAgent <7ee3410c25e4372d84e93318fe42696b>          471              471                      (daemon)
     debugserver <befb5a346ad43ac1be6790ab46eaceff>            0                0                      (daemon)
springboardservi <438ae856a1c039a197e6987a99a8b9b6>            0                0                      (daemon)
   syncdefaultsd <a18b8aeea66e3ebb9ab0d54e55c1b9d1>          195              195                      (daemon)
            afcd <3bd960d39c9f3972a10923db0e687b4a>          147              147                      (daemon)
    syslog_relay <ee47daae952636649230d38b3dc5c2e0>            0                0                      (daemon)
notification_pro <08df322fbe7739199f78852a511169ef>          129              129                      (daemon)
            afcd <3bd960d39c9f3972a10923db0e687b4a>          139              139                      (daemon)
             ubd <da941d158131359ab82559fdf8b99cb7>          347              347                      (daemon)
       CVMServer <0944339d86d03b94bf9deda6eecf5642>           72               72                      (daemon)
filecoordination <28ca39773e933ed2aa92c117c661d056>          129              129                      (daemon)
       distnoted <699b8253736233b29180419f139e8c01>          106              106                      (daemon)
            apsd <5be27dc5ea5234319bc4b47380e42174>          304              304                      (daemon)
      aggregated <c5a375854c3c31d59548ab53ea86d194>           88               88                      (daemon)
        networkd <49064febbe553338bd98051399022da4>          181              181                      (daemon)
       fseventsd <3ea853cb95de3aa48972aa42637af69a>          295              295                      (daemon)
        BTServer <af0150d5085e326598edff072a23d146>          436              436                      (daemon)
         imagent <2b64c6111aa63b179a15afd6a76a6696>          420              420                      (daemon)
   mDNSResponder <1e651badfb7033a68a73a667ec480a08>          210              210                      (daemon)
CommCenterClassi <19c784a3f93a35208de01bd47343facc>          637              637                      (daemon)
         notifyd <67a17b0c297e3785a9e09b8e72f3636a>          186              186                      (daemon)
     ReportCrash <68e323272a9d37c58ba4cdf1279764c4>          242              352                      (daemon)

**End**
user_Dennis_Mostajo
  • 2,279
  • 4
  • 28
  • 38
  • 1
    Add crash log please. – Groot Jun 19 '13 at 20:41
  • Well, in the iOS Simulator run in normal way, but when I run in the Device, the app crashes & say 2013-06-19 18:20:19.986 Samsom[6096:907] magnitude: 93.718727, slideMult: 0.468594 2013-06-19 18:20:43.109 Samsom[6096:907] Received memory warning. – user_Dennis_Mostajo Jun 20 '13 at 13:12

1 Answers1

0

There are a few possible issues at play here:

  1. One issue is that imageNamed will cache the images. You might consider using imageWithContentsOfFile instead. As the imageNamed documentation says:

    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.

    Thus, if you use an image for a one-time (or infrequently used) animation, consider using imageWithContentsOfFile rather than imageNamed.

  2. When the animation is done (which can be detected by checking isAnimating property, such as in a CADisplayLink), you can nil the animationImages property of the UIImageView, which will release the strong reference to those images.

  3. Are the images a size appropriate for the UIImageView? If you have an image whose size (measured in pixels, not by file size) exceeds the size of the image view, iOS will gracefully resize the image in the view (depending upon your contentMode), but it will still store the uncompressed image in memory (usually taking up four bytes per pixel). Don't look at the size of the PNG/JPG file to consider the memory implications (because those are compressed file formats that are uncompressed in memory when you use them). Look at the dimensions of the underlying UIImage.

    If the image dimensions exceed what is needed by the image view, you might want to resize them. This can, depending upon the size of your images, have a dramatic impact on the memory consumption. There are many image resizing algorithms out there (such as this).

Community
  • 1
  • 1
Rob
  • 415,655
  • 72
  • 787
  • 1,044