2013-06-19 84 views
0

我在我的應用程序中使用了相同類型的空白和函數,並且在我的設備或模擬器上運行時有很多動畫(例如70個,10個在7個視圖控制器中),導致應用程序崩潰。
如何使用線程或調度來改進這些動畫,或者問題是內存?
怎麼可能使用來完成這一點,在另一個問題&教程,同樣的方法對動畫&都非常簡單,代碼是這樣的,我重複了,我需要在我的應用程序的任何動畫:收到的內存警告:爲什麼我的應用程序崩潰了很多簡單的動畫?

- (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]; 
} 

我正在使用Xcode 4.6 &該應用爲iOS 5.0。即時通訊使用ARC與故事板&模式或推塞格,我做了一個簡單的方法創建這個應用程序,所以如果你知道的東西或幫助我如何修復或改善這個代碼以更好的方式與線程或調度也許?

和O是的,以及崩潰日誌是:

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** 
+1

請加上崩潰日誌。 – Groot

+0

嗯,在iOS模擬器以正常的方式運行,但是當我在設備上運行,應用程序崩潰並說2013-06-19 18:20:19.986 Samsom [6096:907]幅度:93.718727,slideMult:0.468594 2013 -06-19 18:20:43.109 Samsom [6096:907]收到內存警告。 –

回答

0

有在這裏玩了幾個可能的問題:

  1. 一個問題是,imageNamed將緩存圖像。您可以考慮使用imageWithContentsOfFile。由於imageNamed文件說:

    如果將只顯示一次的圖像文件,並希望確保它不會添加到系統的緩存,你應該使用imageWithContentsOfFile:而是創建你的形象。這將使您的一次性圖像保留在系統映像緩存之外,這可能會改善應用程序的內存使用特性。

    因此,如果使用的圖像獲得一次性(或不經常使用)的動畫,可以考慮使用imageWithContentsOfFile而非imageNamed

  2. 當動畫完成(這可以通過檢查isAnimating屬性,諸如在CADisplayLink來檢測),則可以nilUIImageView,它會釋放出強引用這些圖像的animationImages屬性。

  3. 這些圖像的尺寸是否適用於UIImageView?如果您的圖像的大小(以像素爲單位,而不是文件大小)超過圖像視圖的大小,iOS會優雅地調整視圖中的圖像大小(取決於您的contentMode),但仍會將未壓縮的圖像存儲在內存(通常每個像素佔用四個字節)。不要考慮PNG/JPG文件的大小來考慮內存影響(因爲這些文件格式是使用它們時在內存中未壓縮的壓縮文件格式)。看看底層UIImage的尺寸。

    如果圖片尺寸超過圖片視圖所需的尺寸,您可能需要調整尺寸。這可能會取決於圖像的大小,對內存消耗產生巨大影響。有很多圖像大小調整算法(如this)。

相關問題