2012-04-15 88 views
0

遇到這個問題,我想通過凹凸發送的圖像,但磕碰API的最大塊爲256KB如何NSKeyedUnarchiver與多塊來自凹凸?

if([[NSKeyedArchiver archivedDataWithRootObject:self.selectedImg]length] > 262144) 
    { 
     int dlen = [[NSKeyedArchiver 
        archivedDataWithRootObject:self.selectedImg] length]; 
     NSLog(@"Sending data: %i bytes in %d chunks",dlen, 
       (int)ceil(((float)dlen/262144.0f))); 
     for (int i=1; i <= (int)ceil(((float)dlen/262144.0f)); i++) { 
      int maxr=0; 
      if ((262144*i) > dlen) { 
       maxr = dlen-(262144*(i-1)); 
      } else { 
       maxr = 262144; 
      } 

      NSData *moveChunk = [[NSKeyedArchiver 
            archivedDataWithRootObject:self.selectedImg] 
           subdataWithRange:NSMakeRange(262144*(i-1),maxr)]; 
      NSLog(@"Sending Chunk: %d, %d bytes",i,[moveChunk length]); 
      [bumpObject sendData:moveChunk]; 
     } 
    } 
    else 
    { 
     //Data is 254kb or under 
     NSData *moveChunk = [NSKeyedArchiver 
          archivedDataWithRootObject:self.selectedImg]; 
     [bumpObject sendData:moveChunk]; 
    } 

我如何編譯所有的塊一起?

一直花費3小時,但解決不了。

+1

爲什麼不將'[NSKeyedArchiver archivedDataWithRootObject:self.selectedImg]'的值賦給變量?我認爲創建一個數據對象在系統資源上可能並不是微不足道的......(另外,你爲什麼不直接使用'dlen/262144'?不管怎樣,整數分割都會被截斷)。 – samson 2012-04-15 10:22:41

+0

這是發送數據,這很好。但我需要知道如何從設備的另一側恢復 – Desmond 2012-04-15 10:40:36

+1

這很難以這種方式閱讀。 – samson 2012-04-15 10:45:12

回答

1

好的,我沒有真的嘗試過這個,但是在這裏。

在接收端,以接收NSData的對象,並將它們礫岩成NSMutableData與-[NSMutableData appendData:]-[NSMutableData appendBytes:length:]一個實例。 (得到-[NSData bytes]字節和-[NSData length]

然後,獲得圖像回用+[NSKeyedUnarchiver unarchiveObjectWithData:]

......像這樣(有一個NSMutableData屬性設置,讓我們說叫receivedImageData)...

- (void) bumpDataReceived:(NSData *)chunk { 
    if (!self.receivedImageData) { 
     self.receivedImageData = [NSMutableData dataWithCapacity:[chunk length]]; 
     [self.receivedImageData setData:chunk]; 
    } else { 
     [self.receivedImageData appendData:chunk]; 
    } 
} 

...然後,當你拿到最後一塊(它的大小不等於262144?在-bumpSessionEnded:?)獲取圖像對象回來

UIImage* receivedImage = [NSKeyedUnarchiver unarchiveObjectWithData:self.receivedImageData]; 

完成!

+0

你能給出一個示例代碼? – Desmond 2012-04-15 10:56:03

+1

@Desmond,檢查我的編輯。 – samson 2012-04-15 12:22:42

+0

真棒,它的作品! 很快就會發布我的代碼! 謝謝參孫 – Desmond 2012-04-15 14:02:01