/* 1. Set up the UI interface 2. Introduce the framework 3. Click to select photo 4. Connect the Bluetooth device 5. Implement the proxy method of Bluetooth 6. Send photos */ #import "ViewController.h" #import <GameKit/GameKit.h> @interface ViewController ()<UINavigationControllerDelegate, UIImagePickerControllerDelegate, GKPeerPickerControllerDelegate> @property (nonatomic, strong) UIImageView *imgView; @property (nonatomic, strong) UIButton *selectImgBtn; @property (nonatomic, strong) UIButton *connectionDeviceBtn; @property (nonatomic, strong) UIButton *sendImgBtn; @property (nonatomic, strong) GKSession *seccion; @end @implementation ViewController - (void)viewDidLoad { [super viewDidLoad]; _imgView = [[UIImageView alloc] initWithFrame:CGRectMake(10, 20, self.view.bounds.size.width - 20, self.view.bounds.size.width - 20)]; _imgView.backgroundColor = [UIColor lightGrayColor]; [self.view addSubview:_imgView]; _selectImgBtn = [UIButton buttonWithType:UIButtonTypeSystem]; _selectImgBtn.frame = CGRectMake(10, CGRectGetMaxY(_imgView.frame) + 30, 60, 30); [_selectImgBtn setTitle: @" Select Photo " forState:UIControlStateNormal]; [self.view addSubview:_selectImgBtn]; [_selectImgBtn addTarget:self action:@selector(clickSelectImgBtnAction) forControlEvents:UIControlEventTouchUpInside]; _connectionDeviceBtn = [UIButton buttonWithType:UIButtonTypeSystem]; _connectionDeviceBtn.frame = CGRectMake(0, 0, 60, 30); CGPoint p = CGPointMake(self.view.center.x, _selectImgBtn.center.y); _connectionDeviceBtn.center = p; [_connectionDeviceBtn setTitle: @" connection device " forState:UIControlStateNormal]; [self.view addSubview:_connectionDeviceBtn]; [_connectionDeviceBtn addTarget:self action:@selector(clickConnectionDeviceBtnAction) forControlEvents:UIControlEventTouchUpInside]; _sendImgBtn = [UIButton buttonWithType:UIButtonTypeSystem]; _sendImgBtn.frame = CGRectMake(self.view.bounds.size.width - 70, CGRectGetMinY(_selectImgBtn.frame), 60, 30); [_sendImgBtn setTitle: @" Send photo " forState:UIControlStateNormal]; [self.view addSubview:_sendImgBtn]; [_sendImgBtn addTarget:self action:@selector(clickSendImgBtnAction) forControlEvents:UIControlEventTouchUpInside]; } // Select image - ( void )clickSelectImgBtnAction { // 0. Determine if the photo is available if (! [UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypePhotoLibrary]) { return; } // 1. Create controller UIImagePickerController *ipc = [UIImagePickerController new ]; // 2. Set image source ipc.sourceType = UIImagePickerControllerSourceTypePhotoLibrary; // 3. Set delegate ipc. delegate = self; // 4. Display controller [self presentViewController:ipc animated:YES completion:nil]; } // Connect Device - ( void )clickConnectionDeviceBtnAction { // 1. Create controller GKPeerPickerController *pic = [GKPeerPickerController new ]; // 2. Connect device to get data pic. delegate = self; // 3. Display controller [pic show]; } // Send image - ( void ) clickSendImgBtnAction { // 0. A data object is needed to convert the image into data data // CGFloat compressionQuality precision, compression ratio NSData *data = UIImageJPEGRepresentation(self.imgView.image, 0.2 ); // 1. Send data through session /* datagrams (packets, small chunks) GKSendDataReliable, when the network data is sent incorrectly, it can ensure that the messages arrive in the order sent GKSendDataUnreliable is only sent once */ [self.seccion sendDataToAllPeers:data withDataMode:GKSendDataReliable error:nil]; } Proxy for #pragma Albums - (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary<NSString *,id> *)info { NSLog(@"--info-- = %@", info); UIImage *image = info[UIImagePickerControllerOriginalImage]; _imgView.image = image; [picker dismissViewControllerAnimated:YES completion:nil]; } #pragma mark - GKPeerPickerControllerDelegate - (void)peerPickerController:(GKPeerPickerController *)picker didConnectPeer:(NSString *)peerID toSession:(GKSession *)session { self.seccion = session; // 1. Set the session session, Handler (handle, similar to proxy) [session setDataReceiveHandler:self withContext:nil]; // 2. Remove the controller [picker dismiss]; } // 从setDataReceiveHandler里面找出来 - (void)receiveData:(NSData *)data formPeer:(NSString *)peer inSession:(GKSession *)session context:(void *)context { UIImage *image = [UIImage imageWithData:data]; _imgView.image = image; }