2012-05-03 19 views
8

पर छवि और पाठ भेजने के साथ बग मुझे GameKit ढांचे के साथ कोई समस्या है। मेरे ऐप में, मैं एक टेक्स्ट या एक छवि भेजना चाहता हूं। अब, जब मैं UIImagePicker से एक छवि भेजता हूं, तो यह अच्छी तरह से काम करता है। जब मैं टेक्स्ट भेजता हूं, तो यह छवियों के लिए UIAlertView नहीं दिखाता है, जिसमें कोई छवि नहीं है। यहाँ .h है:आईफोन - ब्लूटूथ

#import <UIKit/UIKit.h> 
#import <GameKit/GameKit.h> 

@interface ViewController : UIViewController <GKPeerPickerControllerDelegate, GKSessionDelegate, UIActionSheetDelegate, UINavigationControllerDelegate, UIImagePickerControllerDelegate> { 
    GKSession *currentSession; 
    IBOutlet UITextField *txtMessage; 
    IBOutlet UIButton *connect; 
    IBOutlet UIButton *disconnect; 
    GKPeerPickerController *picker; 
    UIImagePickerController *imgPicker; 
    BOOL isText; 
    UIImage *_image; 
} 
@property (nonatomic, retain) GKSession *currentSession; 
@property (nonatomic, retain) UITextField *txtMessage; 
@property (nonatomic, retain) UIButton *connect; 
@property (nonatomic, retain) UIButton *disconnect; 
-(IBAction) btnSend:(id) sender; 
-(IBAction) btnConnect:(id) sender; 
-(IBAction) btnDisconnect:(id) sender; 
-(IBAction)chooseImageData:(id)sender; 
@end 

और .m:

#import "ViewController.h" 
#import <GameKit/GameKit.h> 

@implementation ViewController 

@synthesize currentSession; 
@synthesize txtMessage; 
@synthesize connect; 
@synthesize disconnect; 

- (void)didReceiveMemoryWarning 
{ 
    [super didReceiveMemoryWarning]; 
    // Release any cached data, images, etc that aren't in use. 
} 

#pragma mark - View lifecycle 

- (void)viewDidLoad { 
    [connect setHidden:NO]; 
    [disconnect setHidden:YES]; 
    [super viewDidLoad]; 
} 

- (void)peerPickerController:(GKPeerPickerController *)picker 
       didConnectPeer:(NSString *)peerID 
        toSession:(GKSession *) session { 
    self.currentSession = session; 
    session.delegate = self; 
    [session setDataReceiveHandler:self withContext:nil]; 
    picker.delegate = nil; 
    [picker dismiss]; 
} 

-(IBAction) btnConnect:(id) sender { 
    picker = [[GKPeerPickerController alloc] init]; 
    picker.delegate = self; 
    picker.connectionTypesMask = GKPeerPickerConnectionTypeNearby; 
    [connect setHidden:YES]; 
    [disconnect setHidden:NO]; 
    [picker show]; 
} 

- (void)peerPickerControllerDidCancel:(GKPeerPickerController *)picker 
{ 
    picker.delegate = nil; 
    [connect setHidden:NO]; 
    [disconnect setHidden:YES]; 
} 

-(IBAction) btnDisconnect:(id) sender { 
    [self.currentSession disconnectFromAllPeers]; 
    currentSession = nil; 
    [connect setHidden:NO]; 
    [disconnect setHidden:YES]; 
} 

- (void)session:(GKSession *)session 
      peer:(NSString *)peerID 
didChangeState:(GKPeerConnectionState)state { 
    switch (state) 
    { 
     case GKPeerStateConnected: 
      NSLog(@"connected"); 
      break; 
     case GKPeerStateDisconnected: 
      NSLog(@"disconnected"); 
      currentSession = nil; 
      [connect setHidden:NO]; 
      [disconnect setHidden:YES]; 
      break; 
    } 
} 

- (void) mySendDataToPeers:(NSData *) data 
{ 
    if (currentSession) 
     [self.currentSession sendDataToAllPeers:data 
            withDataMode:GKSendDataReliable 
              error:nil]; 
} 
- (void) mySendImageToPeers:(NSData *) data 
{ 
    if (currentSession) 
     [self.currentSession sendDataToAllPeers:data 
            withDataMode:GKSendDataReliable 
              error:nil]; 
} 

-(IBAction) btnSend:(id) sender 
{ 
    //---convert an NSString object to NSData--- 
    NSData* data; 
    NSString *str = [NSString stringWithString:txtMessage.text]; 
    data = [str dataUsingEncoding: NSASCIIStringEncoding]; 
    [self mySendDataToPeers:data]; 
} 

- (void) receiveData:(NSData *)data 
      fromPeer:(NSString *)peer 
      inSession:(GKSession *)session 
      context:(void *)context { 
    //---convert the NSData to NSString--- 

    if (isText) { 
     isText = YES; 
    NSString* str; 
    str = [[NSString alloc] initWithData:data encoding:NSASCIIStringEncoding]; 
    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Messaggio" 
                message:str 
                delegate:self 
              cancelButtonTitle:@"OK" 
              otherButtonTitles:nil]; 
    [alert show]; 
    } 
    else 
    { 
     UIImage *tempSentImg = [UIImage imageWithData:data]; 
     _image = tempSentImg; 
     UIAlertView* alert = [[UIAlertView alloc] initWithTitle:@"Immagine" 
                 message:@"\n\n\n\n\n" 
                 delegate:self 
               cancelButtonTitle:@"OK" 
               otherButtonTitles:nil]; 
     UIImageView *imageView = [[UIImageView alloc] initWithImage:_image]; 
     CGFloat imageHeight = 100; 
     CGFloat imageWidth = imageHeight * _image.size.width/_image.size.height; 
     imageView.frame = CGRectMake(floor((284 - imageWidth)/2), 47, imageWidth, imageHeight); 
     [alert addSubview:imageView]; 
     [alert show]; 
     return; 
    } 

} 

-(IBAction)chooseImageData:(id)sender { 
    [self performSelector: @selector(dialogOtherAction)]; 
} 

- (void)dialogOtherAction { 
    UIActionSheet *actionSheet = [[UIActionSheet alloc] initWithTitle:nil 
                  delegate:self 
                cancelButtonTitle:NSLocalizedString(@"Annulla", @"") 
               destructiveButtonTitle:nil 
                otherButtonTitles:NSLocalizedString(@"Scegli dalla libreria", @""), 
            NSLocalizedString(@"Scatta una foto", @""),nil]; 
    actionSheet.actionSheetStyle = UIActionSheetStyleBlackTranslucent; 
    [actionSheet showInView:self.view]; 
} 

- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex 
{ 
    if (buttonIndex == 0) 
    { 
     imgPicker = [[UIImagePickerController alloc] init]; 
     imgPicker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary; 
     imgPicker.allowsEditing = NO; 
     imgPicker.delegate = self; 

     [self presentModalViewController:imgPicker animated:YES]; 
    } 
    else if (buttonIndex == 1) 
    { 
     imgPicker = [[UIImagePickerController alloc] init]; 
     imgPicker.sourceType = UIImagePickerControllerSourceTypeCamera; 
     imgPicker.delegate = self; 
     imgPicker.allowsEditing = NO; 
     [self presentModalViewController:imgPicker animated:YES]; 
    } 
} 

-(void)actionSheet:(UIActionSheet *)actionSheet didDismissWithButtonIndex:(NSInteger)buttonIndex { 
    [actionSheet dismissWithClickedButtonIndex:0 animated:YES]; 
} 

- (void)imagePickerController:(UIImagePickerController *)imgPicker didFinishPickingMediaWithInfo:(NSDictionary *)info 
{ 
    [self dismissModalViewControllerAnimated:YES];  

    UIImage *image = [info objectForKey:@"UIImagePickerControllerOriginalImage"]; 
    [self performSelector: @selector(btnImageSend:) withObject: image afterDelay: 1.0]; 
} 
-(IBAction) btnImageSend:(UIImage *)image 
{ 
    //---convert an NSString object to NSData--- 
    NSData* data; 
    data = UIImagePNGRepresentation(image); 
    [self mySendImageToPeers:data]; 
} 


- (void)viewDidUnload 
{ 
    [super viewDidUnload]; 
    // Release any retained subviews of the main view. 
    // e.g. self.myOutlet = nil; 
} 

- (void)viewWillAppear:(BOOL)animated 
{ 
    [super viewWillAppear:animated]; 
} 

- (void)viewDidAppear:(BOOL)animated 
{ 
    [super viewDidAppear:animated]; 
} 

- (void)viewWillDisappear:(BOOL)animated 
{ 
    [super viewWillDisappear:animated]; 
} 

- (void)viewDidDisappear:(BOOL)animated 
{ 
    [super viewDidDisappear:animated]; 
} 

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation 
{ 
    // Return YES for supported orientations 
    return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown); 
} 

@end 

उत्तर

0

मुझे लगता है जैसे आप कह क्या प्राप्त अंत में भेजा गया था का कोई रास्ता नहीं है। प्रारंभ में "isText" चर नहीं है। हर बार डेटा प्राप्त होता है (प्राप्त डेटा में: सेपर: इन सत्र: संदर्भ :), यदि (isText) हमेशा विफल रहता है और प्राप्त डेटा हमेशा छवि डेटा के रूप में व्याख्या किया जाता है।

एक तरीका जिसे आप हल कर सकते हैं, केवल पाठ या छवि डेटा भेजने की बजाय, आप एक शब्दकोश भेज सकते हैं। मेरे सैंडडाटा टॉपीर्स में निम्न परिवर्तन करें :, mySendImageToPeers: और प्राप्त करेंडेटा: सेपर: इन सत्र: संदर्भ:

- (void) mySendDataToPeers:(NSData *) data 
{ 
    NSDictionary *dict = @{@"data type" : @"text", 
          @"data" : data}; 
    NSData *dictData = [NSPropertyListSerialization dataWithPropertyList:dict format:NSPropertyListBinaryFormat_v1_0 options:0 error:nil]; 

    if (currentSession) 
     [self.currentSession sendDataToAllPeers:dictData 
            withDataMode:GKSendDataReliable 
              error:nil]; 
} 

- (void) mySendImageToPeers:(NSData *) data 
{ 
    NSDictionary *dict = @{@"data type" : @"image", 
          @"data" : data}; 
    NSData *dictData = [NSPropertyListSerialization dataWithPropertyList:dict format:NSPropertyListBinaryFormat_v1_0 options:0 error:nil]; 

    if (currentSession) 
     [self.currentSession sendDataToAllPeers:dictData 
            withDataMode:GKSendDataReliable 
              error:nil]; 
} 


- (void) receiveData:(NSData *)dictdata 
      fromPeer:(NSString *)peer 
      inSession:(GKSession *)session 
      context:(void *)context 
{ 
    NSDictionary *dict = (NSDictionary *) [NSPropertyListSerialization propertyListWithData:dictdata options:0 format:0 error:nil]; 
    isText = [@"text" isEqualToString:[dict objectForKey:@"data type"]]; 
    NSData *data = [dict objectForKey:@"data"]; 

    //---convert the NSData to NSString--- 
    if (isText) 
    { 
     NSString* str; 
     str = [[NSString alloc] initWithData:data encoding:NSASCIIStringEncoding]; 
     UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Messaggio" 
                 message:str 
                 delegate:self 
               cancelButtonTitle:@"OK" 
               otherButtonTitles:nil]; 
     [alert show]; 
    } 
    else 
    { 
     UIImage *tempSentImg = [UIImage imageWithData:data]; 
     _image = tempSentImg; 
     UIAlertView* alert = [[UIAlertView alloc] initWithTitle:@"Immagine" 
                 message:@"\n\n\n\n\n" 
                 delegate:self 
               cancelButtonTitle:@"OK" 
               otherButtonTitles:nil]; 
     UIImageView *imageView = [[UIImageView alloc] initWithImage:_image]; 
     CGFloat imageHeight = 100; 
     CGFloat imageWidth = imageHeight * _image.size.width/_image.size.height; 
     imageView.frame = CGRectMake(floor((284 - imageWidth)/2), 47, imageWidth, imageHeight); 
     [alert addSubview:imageView]; 
     [alert show]; 
     return; 
    } 

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