2015-07-28 51 views
1

我使用繪圖實時心電圖 CorePlot library.When它iPad的空氣當我與iPad的迷你試過性能okay.But,還有一個用途延遲繪圖。我已經完成了與collapseLayer和this link也沒有解決我的問題。任何人都可以爲此提出新的解決方案。提高性能的核心情節

我的代碼如下:

-(void)newData:(NSTimer *)theTimer 
{ 

    for (int i =0;i<plotcount;i++){ 

     if([Qrrch0 count]>0 || [Qrrch1 count]>0 || [Qrrch2 count]>0 || [Qrrch3 count]>0 || [Qrrspo2 count]>0){ 

      if(g1==1 && thePlot){ 

       currentIndex ++; 
      } 
      if(g2==1 && thePlot1){ 
       currentIndex1 ++; 
      } 
      if(g3==1 && thePlot2){ 
       currentIndex2 ++; 
      } 
      if(g4==1 && thePlot3){ 
       currentIndex3 ++; 
      } 
      if(spo2==1 && thePlot4){ 
       currentIndex4 ++; 
      } 

      if(arrayIndex>=kchannel1-1) 
      { 
       arrayIndex=0; 
      } 

      if(arrayIndex1>=kchannel2-1) 
      { 
       arrayIndex1=0; 
      } 
      if(arrayIndex>=kchannel1-1) 
      { 
       arrayIndex=0; 
      } 
      if(arrayIndex2>=kchannel3-1) 
      { 
       arrayIndex2=0; 
      } 

      if(arrayIndex3>=kchannel4-1) 
      { 
       arrayIndex3=0; 
      } 

      if(arrayIndex4>=kchannel5-1) 
      { 
       arrayIndex4=0; 
      } 




      if(g1==1 && thePlot){ 
       currentIndex5++; 
       if(currentIndex5>=kchannel1) 
       { 
        if(arrayIndex==0) 
        { 
         [thePlot reloadDataInIndexRange:NSMakeRange(arrayIndex, arrayIndex)]; 
        }else{ 
        [thePlot deleteDataInIndexRange:NSMakeRange(arrayIndex, 1)]; 


        } 
       } 
       if([Qrrch0 count]!=0) 
       { 

        arrPlot[arrayIndex]=[[Qrrch0 objectAtIndex:0] integerValue]; 

        lastPlot0=[Qrrch0 objectAtIndex:0]; 

       }else{ 
        arrPlot[arrayIndex]=[lastPlot0 integerValue]; 

       } 
        arrayIndex++; 
      } 
      if(g2==1 && thePlot1){ 
       currentIndex6++; 
       if(currentIndex6>=kchannel2) 
       { 
        if(arrayIndex1==0) 
        { 
         [thePlot1 reloadDataInIndexRange:NSMakeRange(arrayIndex1, arrayIndex1)]; 
        }else{ 
         [thePlot1 deleteDataInIndexRange:NSMakeRange(arrayIndex1, 1)]; 
        } 

       } 
       if([Qrrch1 count]!=0) 
       { 


        arrPlot1[arrayIndex1]=[[Qrrch1 objectAtIndex:0] integerValue]; 

        lastPlot1=[Qrrch1 objectAtIndex:0]; 

       }else{ 
        arrPlot1[arrayIndex1]=[lastPlot1 integerValue]; 

       } 
       arrayIndex1++; 

      } 
      if(g3==1 && thePlot2){ 
       currentIndex7++; 

       if(currentIndex7>=kchannel3) 
       { 
        if(arrayIndex2==0) 
        { 
         [thePlot2 reloadDataInIndexRange:NSMakeRange(arrayIndex2, arrayIndex2)]; 
        }else{ 
         [thePlot2 deleteDataInIndexRange:NSMakeRange(arrayIndex2, 1)]; 
        } 

       } 
       if([Qrrch2 count]!=0) 
       { 

        arrPlot2[arrayIndex2]=[[Qrrch2 objectAtIndex:0] integerValue]; 
        lastPlot2=[Qrrch2 objectAtIndex:0]; 

       }else{ 
        arrPlot2[arrayIndex2]=[lastPlot2 integerValue]; 
            } 
        arrayIndex2++; 
      } 
      if(g4==1 && thePlot3){ 
       currentIndex8++; 

       if(currentIndex8>=kchannel4) 
       { 
        if(arrayIndex3==0) 
        { 
         [thePlot3 reloadDataInIndexRange:NSMakeRange(arrayIndex3, arrayIndex3)]; 
        }else{ 
         [thePlot3 deleteDataInIndexRange:NSMakeRange(arrayIndex3, 1)]; 
        } 

       } 

       if([Qrrch3 count]!=0) 
       { 

        arrPlot3[arrayIndex3]=[[Qrrch3 objectAtIndex:0] integerValue]; 
        lastPlot3=[Qrrch3 objectAtIndex:0]; 

       }else{ 
        arrPlot3[arrayIndex3]=[lastPlot3 integerValue]; 
            } 
       arrayIndex3++; 
      } 
      if(spo2==1 && thePlot4){ 
       currentIndex9++; 
       if(currentIndex9>=kchannel5) 
       { 
        if(arrayIndex4==0) 
        { 
         [thePlot4 reloadDataInIndexRange:NSMakeRange(arrayIndex4, arrayIndex4)]; 
        }else{ 
         [thePlot4 deleteDataInIndexRange:NSMakeRange(arrayIndex4, 1)]; 
        } 

       } 
       if([Qrrspo2 count]!=0) 
       { 

        arrPlot4[arrayIndex4]=[[Qrrspo2 objectAtIndex:0] integerValue]; 

        lastPlot4=[Qrrspo2 objectAtIndex:0]; 

       }else{ 
        arrPlot4[arrayIndex4]=[lastPlot4 integerValue]; 


       } 
       arrayIndex4++; 
      } 

      if(g1==1 && thePlot){ 
       if([Qrrch0 count]!=0) 
       { 
        [Qrrch0 removeObjectAtIndex:0]; 
       } 
       if(currentIndex>=kchannel1) 
       { 
       currentIndex=1; 
            } 
        [thePlot insertDataAtIndex:currentIndex-1 numberOfRecords:1]; 
      } 
      if(g2==1 && thePlot1){ 
       if([Qrrch1 count]!=0) 
       { 
        [Qrrch1 removeObjectAtIndex:0]; 
       } 

       if(currentIndex1>=kchannel2) 
       { 
        currentIndex1=1; 

       } 
        [thePlot1 insertDataAtIndex:currentIndex1-1 numberOfRecords:1]; 
       } 
      if(g3==1 && thePlot2){ 

       if([Qrrch2 count]!=0) 
       { 
        [Qrrch2 removeObjectAtIndex:0]; 
       } 
       if(currentIndex2>=kchannel3) 
       { 
        currentIndex2=1; 
      } 
        [thePlot2 insertDataAtIndex:currentIndex2-1 numberOfRecords:1]; 
      } 

      if(g4==1 && thePlot3){ 
       if([Qrrch3 count]!=0) 
       { 
        [Qrrch3 removeObjectAtIndex:0]; 
       } 
       if(currentIndex3>=kchannel4) 
       { 
        currentIndex3=1; 
       } 
       [thePlot3 insertDataAtIndex:currentIndex3-1 numberOfRecords:1]; 

      } 

      if(spo2==1 && thePlot4){ 
       if([Qrrspo2 count]!=0) 
       { 
        [Qrrspo2 removeObjectAtIndex:0]; 
       } 
       if(currentIndex4>=kchannel5) 
       { 
        currentIndex4=1; 
       } 
       [thePlot4 insertDataAtIndex:currentIndex4-1 numberOfRecords:1]; 

      } 

        } 
     else 
     { 

      [self datacha]; 


     } 
    } 

} 


-(void) datacha{ 
    NSArray *array; 
    if([FinalArray count]>0){ 

     if(g1==1 && thePlot){ 
      array = [[NSArray alloc] initWithArray:[FinalArray objectAtIndex:0]]; 
      [Qrrch0 addObjectsFromArray:array]; 
     } 
     if(g1==1 && thePlot){ 
      [FinalArray removeObjectAtIndex:0]; 
     } 
    } 
    if([FinalArray1 count]>0){ 
     if(g2==1 && thePlot1){ 
      array = [[NSArray alloc] initWithArray:[FinalArray1 objectAtIndex:0]]; 
      [Qrrch1 addObjectsFromArray:array]; 
     } 
     if(g2==1 && thePlot1){ 
      [FinalArray1 removeObjectAtIndex:0]; 
     } 
    } 
    if([FinalArray2 count]>0){ 
     if(g3==1 && thePlot2){ 

      array = [[NSArray alloc] initWithArray:[FinalArray2 objectAtIndex:0]]; 
      [Qrrch2 addObjectsFromArray:array]; 
     } 
     if(g3==1 && thePlot2){ 
      [FinalArray2 removeObjectAtIndex:0]; 
     } 
    } if([FinalArray3 count]>0){ 
     if(g4==1 && thePlot3){ 
      array = [[NSArray alloc] initWithArray:[FinalArray3 objectAtIndex:0]]; 
      [Qrrch3 addObjectsFromArray:array]; 
     } 
     if(g4==1 && thePlot3){ 
      [FinalArray3 removeObjectAtIndex:0]; 
     } 
    } 
    if([FinalArray4 count]>0){ 
     if(spo2==1 && thePlot4){ 
      array = [[NSArray alloc] initWithArray:[FinalArray4 objectAtIndex:0]]; 
      [Qrrspo2 addObjectsFromArray:array]; 
     } 
     if(spo2==1 && thePlot4){ 
      [FinalArray4 removeObjectAtIndex:0]; 
     } 
    } 
} 

#pragma mark - 
#pragma mark Plot Data Source Methods 


-(NSUInteger)numberOfRecordsForPlot:(CPTPlot *)plot 
{ 

    if(plot == thePlot) 
    { 
     if(currentIndex5>=kchannel1) 
     { 
      return kchannel1; 
     }else{ 
     return currentIndex5; 
     } 
    } 

    if(plot == thePlot1) 
    { 
     if(currentIndex6>=kchannel2) 
     { 
      return kchannel2; 
     }else{ 
      return currentIndex6; 
     } 
    } 

    if(plot == thePlot2) 
    { 
     if(currentIndex7>=kchannel3) 
     { 
      return kchannel3; 
     }else{ 
      return currentIndex7; 
     } 
    } 

    if(plot == thePlot3) 
    { 
     if(currentIndex8>=kchannel4) 
     { 
      return kchannel4; 
     }else{ 
      return currentIndex8; 
     } 
    } 

    if(plot == thePlot4) 
    { 
     if(currentIndex9>=kchannel5) 
     { 
      return kchannel5; 
     }else{ 
      return currentIndex9; 
     } 
    } 

    return 0; 
} 



-(double)doubleForPlot:(CPTPlot *)plot field:(NSUInteger)fieldEnum recordIndex:(NSUInteger)idx{ 

    double num; 

    NSString *textval = [NSString stringWithFormat:@"%d", countofpktloss]; 
    self.losscount.text = textval; 

    switch (fieldEnum) { 

     case CPTScatterPlotFieldX: 
      if(plot == thePlot){ 
       num =currentIndex; 
      } 
         else if(plot == thePlot1) 
         { 

          num = currentIndex1; 

         } 
         else if(plot == thePlot2) 
         { 

          num = currentIndex2; 

         } 
         else if(plot == thePlot3) 
         { 

          num = currentIndex3; 

         } 

         else if(plot == thePlot4) 
         { 

          num = currentIndex4; 

         } 

      break; 

     case CPTScatterPlotFieldY: 
      if(plot == thePlot){ 

       num =arrPlot[idx]; 

      } 

         else 
        if(plot == thePlot1){ 

         num =arrPlot1[idx]; 


         } 
         else if(plot == thePlot2){ 

          num =arrPlot2[idx]; 


         } 
         else if(plot == thePlot3) 
         { 
          num =arrPlot3[idx]; 


         } 

         else if(plot == thePlot4) 
         { 
          num =arrPlot4[idx]; 

         } 

      break; 

     default: 
      break; 

    } 

    return num; 
} 

回答

1

collapsesLayers屬性是有幫助節省內存的靜態圖。在頻繁更新的圖上使用它會使性能變得更糟,因爲它要求圖重繪所有內容,而不僅僅是更改的部分,例如圖。


添加的註釋

  1. 確保當你期望它的計時器停止。否則,它將繼續爲這些圖添加點。

  2. 定時器多久會觸發一次以將點添加到圖中?每秒更新圖表超過60次是沒有意義的,您可能需要在iPad 2等較早的設備上進一步減少圖表,以獲得具有大量數據點的良好性能。如果需要,您可以在每個更新中添加多個點。

  3. 檢查用於重新加載數據點的範圍。您傳遞的數字與範圍的位置和長度相同。我懷疑你的意思是爲每個範圍使用一個(1)的長度。如果是這樣,那麼您在每次傳遞時都會重新載入比所需更多的數據。

+0

當'collapsesLayers = NO' CPU使用率low.But情節被延遲的技術主管。 當'collapsesLayers = YES' CPU使用率有點高但繪圖沒有延遲。 –

+0

延遲發生在哪裏? –

+0

即使當我停止輸入圖形時,圖形仍然繪圖。在此之前,當輸入停止時,圖形立即停止。 –