2012-04-19 1 views
8

도움이 필요합니다. Bump를 통해 한 iPhone에서 다른 iPhone으로 Jpg 또는 PNG 이미지를 전송하고 싶습니다. 나는 이미지가 전혀 보내지지 않는 성공과 실패를 만났다.Bump Api 송신에 대한 도움이 필요합니다.

은 사용자가 UIimagepicker에서 이미지를 선택할 때 호출 될 NSObject 파일입니다.

수신자는 데이터를 보내지 않고 수신 만합니다.

코드를 살펴보고 의견이나 요령을 알려주십시오.

귀하의 도움에 감사드립니다.

- (id) init{ 
    if(self = [super init]){ 
     bumpObject = [BumpAPI sharedInstance]; 
     NSError *error; 
     NSURL *fileURL = [NSURL fileURLWithPath:[[NSBundle mainBundle]pathForResource:@"sound_bump_tap" ofType:@"aif"]]; 
     bumpsound = [[AVAudioPlayer alloc] initWithContentsOfURL:fileURL error:&error]; 
     [bumpsound prepareToPlay]; 
    } 
    return self; 
} 

-(void) configBump 
{ 

    [bumpObject configAPIKey:@"My API Key"]; //put your api key here. Get an api key from http://bu.mp 
    [bumpObject configDelegate:self]; 
    [bumpObject configParentView:self.bumpShare.view]; 
    [bumpObject configActionMessage:@"Bump with your friend to start."];  
} 

- (void) startBump{ 
    [self configBump]; 
    [bumpObject requestSession]; 
} 

- (void) stopBump{ 
    [bumpObject endSession]; 
} 

#pragma mark - 
#pragma mark Private Methods 

// for Debug -- prints contents of NSDictionary 
-(void)printDict:(NSDictionary *)ddict { 
    NSLog(@"---printing Dictionary---"); 
    NSArray *keys = [ddict allKeys]; 
    for (id key in keys) { 
     NSLog(@" key = %@  value = %@",key,[ddict objectForKey:key]); 
    } 
} 

#pragma mark - 
#pragma mark Public Methods 
- (void) sendDetails:(UIImage *)selectedImage 
{ 
    [bumpShare showHUD]; 
    //Now we need to package our message dictionary up into an NSData object so we can send it up to Bump. 
    //We'll do that with with an NSKeyedArchiver. 
    NSLog(@"Here Got called!!!!!!! %@",self.selectedImg); 




// NSData* wholeImageData = [NSKeyedArchiver archivedDataWithRootObject:userChunk]; 
// int dataLength = [wholeImageData length]; 
// int maxChunkSize = 262144; 
// int chunkCount = dataLength/maxChunkSize; 
//  
// if (chunkCount == 1) { 
//  //Data is 254kb or under 
//  NSData *moveChunk = [NSKeyedArchiver 
//        archivedDataWithRootObject:self.selectedImg]; 
//  [bumpObject sendData:moveChunk]; 
// } 
// else if (dataLength > maxChunkSize) 
// { 
//  NSLog(@"Sending data: %d bytes in %d chunks", dataLength, chunkCount); 
//  for (int i = 1; i <= chunkCount; i++) 
//  { 
//   int ithChunkLength = 0; 
//   if ((maxChunkSize * i) > dataLength) 
//   { 
//    ithChunkLength = dataLength-(maxChunkSize*(i-1)); 
//   } 
//   else { 
//    ithChunkLength = 262144; 
//   } 
//   NSData *moveChunk = [wholeImageData subdataWithRange:NSMakeRange(maxChunkSize*(i-1),ithChunkLength)]; 
//   //[[NSKeyedArchiver archivedDataWithRootObject:self.selectedImg] subdataWithRange:NSMakeRange(262144*(i-1),maxr)]; 
//   NSLog(@"Sending Chunk: %d, %d bytes",i,[moveChunk length]); 
//   [bumpObject sendData:moveChunk]; 
//  } 
// } 

    NSData *photoData = UIImageJPEGRepresentation(self.selectedImg, 0.9); 
    //NSData *userChunk = [NSKeyedArchiver archivedDataWithRootObject:self.selectedImg]; 
    if([[NSKeyedArchiver archivedDataWithRootObject:photoData]length] > 262144) 
    { 
     int dlen = [[NSKeyedArchiver 
        archivedDataWithRootObject:photoData] 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:photoData] 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:photoData]; 
     [bumpObject sendData:moveChunk]; 
    } 


    //[self printDict:moveDict]; 
    //[userDict release]; 

    //Calling send will have bump send the data up to the other user's mailbox. 
    //The other user will get a bumpDataReceived: callback with an identical NSData* chunk shortly. 
    //packetsAttempted++; 
    //[bumpObject sendData:userChunk]; 
} 

- (void) startConnection:(UIImage *)selectedImage 
{ 
    //set local and remote user names 
    //[bumpShare setLocalUserName:[[bumpObject me] userName]]; 
    //[bumpShare setRemoteUserName:[[bumpObject otherBumper] userName]]; 
    //[bumpShare updateUserNames]; 
    [self sendDetails:selectedImage]; 
} 

#pragma mark Utility 
-(void) quickAlert:(NSString *)titleText msgText:(NSString *)msgText{ 
    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:titleText message:msgText delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil]; 
    [alert show]; 
    //[alert release]; 
} 

- (void)imageSavedToPhotosAlbum:(UIImage *)image didFinishSavingWithError:(NSError *)error contextInfo:(void *)contextInfo { 

    NSString *message; 
    NSString *title; 

    if (!error) { 
     title = NSLocalizedString(@"Save Success", @""); 
     message = NSLocalizedString(@"Save Success Message", @""); 
     HUD.customView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"Checkmark.png"]]; 
     HUD.mode = MBProgressHUDModeCustomView; 
     HUD.labelText = @"Photo Saved To Photo Album"; 
     [HUD hide:YES afterDelay:1.5]; 
     //saved =1; 
     //self.imageOverlay.alpha =0.7; 
     [bumpShare hideHUD]; 
     [self performSelector:@selector(saveSuccess) withObject:nil afterDelay:0.5]; 



    } else 
    { 
     title = NSLocalizedString(@"Save Failed", @""); 
     message = [error description]; 

     HUD.customView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"sad_face.png"]]; 
     HUD.mode = MBProgressHUDModeCustomView; 
     HUD.labelText = @"Error Saving to Photo Album"; 
     [HUD hide:YES afterDelay:3]; 
    } 
} 

-(void)saveSuccess 
{ 
    [bumpShare pushToSuccess]; 
} 

#pragma mark - 
#pragma mark BumpAPIDelegate methods 

- (void) bumpDataReceived:(NSData *)chunk 
{ 
    //The chunk was packaged by the other user using an NSKeyedArchiver, so we unpackage it here with our NSKeyedUnArchiver 
     NSLog(@"chunk length %i",[chunk length]); 
     //NSData *receivedData = [NSKeyedUnarchiver unarchiveObjectWithData:chunk]; 
     if ([chunk length] != 262144) 
     { 
      NSLog(@"called length %i",[receivedData length]); 

      if (!self.receivedData) { 
       self.receivedData = [NSMutableData dataWithCapacity:[chunk length]]; 
       [self.receivedData setData:chunk]; 
      } 
      else 
      { 
       [self.receivedData appendData:chunk]; 
      } 
      [self stopBump]; 
      //UIImage* receivedImage = [NSKeyedUnarchiver unarchiveObjectWithData:self.receivedData]; 
      //UIImageWriteToSavedPhotosAlbum(receivedImage, self, @selector(imageSavedToPhotosAlbum: didFinishSavingWithError: contextInfo:), nil); 

     } 
     else if([chunk length] == 262144) 
     { NSLog(@"called length %i",[receivedData length]); 
      //NSLog(@"calledin length %i",[chunk length]); 

      if (!self.receivedData) { 
       self.receivedData = [NSMutableData dataWithCapacity:[chunk length]]; 
       [self.receivedData setData:chunk]; 
      } 
      else 
      { 
       [self.receivedData appendData:chunk]; 
      } 
     } 
} 

- (void) bumpSessionStartedWith:(Bumper*)otherBumper{ 
    [self startConnection:nil]; 
} 

- (void) bumpSessionEnded:(BumpSessionEndReason)reason { 
    NSString *alertText; 
    switch (reason) { 
     case END_LOST_NET: 
      alertText = @"Connection to Bump server was lost."; 
      break; 
     case END_OTHER_USER_LOST: 
      alertText = @"Connection to other user was lost."; 
      break; 
     case END_USER_QUIT: 
      alertText = @"You have been disconnected."; 
      break; 
     default: 
      alertText = @"You have been disconnected."; 
      break; 
    } 

// if(reason != END_USER_QUIT){ 
//  //if the local user initiated the quit,restarting the app is already being handled 
//  //other wise we'll restart here 
//  UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Disconnected" message:alertText delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil]; 
//  [alert show]; 
//  //[alert release]; 
// } 
    NSLog(@"Sending Chun!!!!!!!!!"); 
    NSLog(@"self.received data %i",[self.receivedData length]); 

    if ([self.receivedData length]>200) 
    { 
     NSData *imgData= [NSKeyedUnarchiver unarchiveObjectWithData:self.receivedData]; 
     UIImage* receivedImage = [UIImage imageWithData:imgData];//[NSKeyedUnarchiver unarchiveObjectWithData:self.receivedData]; 
     UIImageWriteToSavedPhotosAlbum(receivedImage, self, @selector(imageSavedToPhotosAlbum: didFinishSavingWithError: contextInfo:), nil); 
    } 
    else 
    { 
     [bumpShare hideHUD]; 
     [self performSelector:@selector(saveSuccess) withObject:nil afterDelay:1.5];   
    } 


} 

- (void) bumpSessionFailedToStart:(BumpSessionStartFailedReason)reason { 

    NSString *alertText; 
    switch (reason) { 
     case FAIL_NETWORK_UNAVAILABLE: 
      alertText = @"Please check your network settings and try again."; 
      break; 
     case FAIL_INVALID_AUTHORIZATION: 
      //the user should never see this, since we'll pass in the correct API auth strings. 
      //just for debug. 
      alertText = @"Failed to connect to the Bump service. Auth error."; 
      break; 
     default: 
      alertText = @"Failed to connect to the Bump service."; 
      break; 
    } 

    if(reason != FAIL_USER_CANCELED){ 
     //if the user canceled they know it and they don't need a popup. 
     UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Connection Failed" message:alertText delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil]; 
     [alert show]; 
     //[alert release]; 
    } 
} 
+1

많은 코드, 정확히 무엇이 잘못 될지 지정할 수 있습니까? 오류 메시지 또는 오류 로그? 254kb 이하 및 254kb 이상의 이미지를 사용해 보셨습니까? 어쩌면 당신은'startConnection :'에 어떤 정보를 놓치고있는 것일까 요? –

+0

안녕하세요. 답장을 보내 주시면 죄송합니다. 앱이 무작위로 송수신되는 것만 큼 크래시가 발생하지 않습니다. 내가 왜 물었는지 그 이유가 너무 확실하지 않습니다. 나는 254kb 이하로 시도하지는 않았지만 위의 254kb 이미지와 함께 작동했습니다. – Desmond

+0

이전에 Bump API를 사용했고 셀룰러 연결과 약한 wifi 신호를 사용했습니다. 두 기기를 Wi-Fi에 연결 한 상태에서 시도해보고 문제가 지속되는지 확인합니다. – skram

답변

2

이미지를 전송하거나 그 256k 제한을 초과하는 데이터를 Bump를 통해 전송하는 것이 매우 느립니다. 페이로드를 덩어리로 버스트 한 다음 각 덩어리가 보내고받을 때 사이에 지연이 있습니다. 당신이 당신의 자신의 웹 서버가있는 경우 여기에 내가 무엇을 할 것이라고입니다 :

  1. 가에 ID 나 경로를 반환 앱을
  2. 웹 서비스가 웹 서비스의 일종을 통해 웹 서버에 이미지를 업로드 되세요 업로드 된 파일에서 가장
  3. 이 응용 프로그램의 수신기 부분은 웹 서버에서 이미지를 가져 오는 경우에만 몇 K가 OVER 정보
  4. BUMP

전송 속도가 얼마나 빨라지는지 놀랄 것입니다.