2012-04-19 9 views
8

मुझे यहां कुछ मदद की ज़रूरत है। मैं एक आईफोन से दूसरे में एक जेपीजी या पीएनजी छवियों को बंप के माध्यम से स्थानांतरित करना चाहता हूं। मुझे सफलता और असफलता का सामना करना पड़ा जहां छवियां बिल्कुल नहीं भेजी गईं।छवियों को भेजने के लिए बंप अपी पर कुछ मदद की ज़रूरत है

नीचे एक NSObject फ़ाइल है जिसे उपयोगकर्ता द्वारा UIimagepicker से छवियों का चयन करने पर कॉल किया जाएगा।

रिसीवर कोई भी डेटा नहीं भेजा जाएगा बल्कि केवल प्राप्त होगा।

कृपया मुझे कोड पर नज़र डालने में मदद करें और मुझे कोई टिप्पणी या बिंदु दें।

धन्यवाद और आपकी सहायता की सराहना की।

- (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

अधिक कोड, क्या आप निर्दिष्ट कर सकते हैं कि वास्तव में क्या गलत है? त्रुटि संदेश या क्रैश लॉग? क्या आपने नीचे और 254 केबी से ऊपर की छवियों की कोशिश की है? शायद आप 'startConnection:' में कुछ जानकारी खो रहे हैं? –

+0

हाय relikd, देर से जवाब के लिए खेद है। ऐप क्रैश नहीं हो रहा है कि यह यादृच्छिक रूप से भेजा और प्राप्त होगा। यह भी सुनिश्चित नहीं है कि मैंने क्यों पूछा। मैंने 254 केबी से नीचे नहीं आज़माया, लेकिन यह निश्चित रूप से उपरोक्त 254 केबी छवियों के साथ काम करता है – Desmond

+0

मैंने पहले बंप एपीआई और सेलुलर कनेक्शन और कमजोर वाईफाई सिग्नल पर इसकी स्पॉटी का उपयोग किया है। वाईफाई से जुड़े दोनों उपकरणों के साथ प्रयास करें, और देखें कि समस्या बनी रहती है या नहीं। – skram

उत्तर

2

मैंने पाया है कि 256k सीमा से अधिक छवियों को स्थानांतरित करने या किसी भी मात्रा में डेटा को टक्कर के माध्यम से, बहुत धीमा है। आपको पर्लोड को हिस्सों में बस्ट करना होगा और फिर प्रत्येक खंड को भेजे जाने और प्राप्त होने के बीच में देरी होगी। आप अपने स्वयं के वेब सर्वर है, तो यहाँ मैं क्या कर सकता है:

  1. एप्लिकेशन वेब सेवा
  2. वेब सेवा है किसी प्रकार का के माध्यम से अपने वेब सर्वर पर चित्र अपलोड करने के लिए एक आईडी या पथ वापसी अपलोड की गई फ़ाइल
  3. टक्कर है कि जानकारी से अधिक है जो केवल कुछ कश्मीर में सबसे अच्छा
  4. अनुप्रयोग के रिसीवर भाग अपने वेब सर्वर से छवि लाने है है

आप आश्चर्यचकित होंगे कि आपका स्थानांतरण कितना तेज़ होगा!

संबंधित मुद्दे