/*
1.设置UI界面
2.引入框架
3.点击选择照片
4.连接蓝牙设备
5.实现蓝牙的代理方法
6.发送照片
*/ #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; // first deprecated in iOS 7.0  @end @implementation ViewController - (void)viewDidLoad {
[super viewDidLoad]; _imgView = [[UIImageView alloc] initWithFrame:CGRectMake(, , self.view.bounds.size.width - , self.view.bounds.size.width - )];
_imgView.backgroundColor = [UIColor lightGrayColor];
[self.view addSubview:_imgView]; _selectImgBtn = [UIButton buttonWithType:UIButtonTypeSystem];
_selectImgBtn.frame = CGRectMake(, CGRectGetMaxY(_imgView.frame) + , , );
[_selectImgBtn setTitle:@"选择照片" forState:UIControlStateNormal];
[self.view addSubview:_selectImgBtn];
[_selectImgBtn addTarget:self action:@selector(clickSelectImgBtnAction) forControlEvents:UIControlEventTouchUpInside]; _connectionDeviceBtn = [UIButton buttonWithType:UIButtonTypeSystem];
_connectionDeviceBtn.frame = CGRectMake(, , , );
CGPoint p = CGPointMake(self.view.center.x, _selectImgBtn.center.y);
_connectionDeviceBtn.center = p;
[_connectionDeviceBtn setTitle:@"连接设备" 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 - , CGRectGetMinY(_selectImgBtn.frame), , );
[_sendImgBtn setTitle:@"发送照片" forState:UIControlStateNormal];
[self.view addSubview:_sendImgBtn];
[_sendImgBtn addTarget:self action:@selector(clickSendImgBtnAction) forControlEvents:UIControlEventTouchUpInside];
} // 选择图片
- (void)clickSelectImgBtnAction { // 0.判断照片是否可用
if (![UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypePhotoLibrary]) { return;
}
// 1.创建控制器
UIImagePickerController *ipc = [UIImagePickerController new];
// 2.设置图片源
ipc.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
// 3.设置代理
ipc.delegate = self;
// 4.显示控制器
[self presentViewController:ipc animated:YES completion:nil]; } // 连接设备
- (void)clickConnectionDeviceBtnAction { // 1.创建控制器
GKPeerPickerController *pic = [GKPeerPickerController new]; // first deprecated in iOS 7.0
// 2.连接设备获取数据
pic.delegate = self;
// 3.显示控制器
[pic show];
} // 发送图片
- (void)clickSendImgBtnAction { // 0.需要一个data对象,把图片转化成data数据
// CGFloat compressionQuality 精度,压缩比
NSData *data = UIImageJPEGRepresentation(self.imgView.image, 0.2);
// 1.通过session发送数据
/*
数据报(数据包,小块)
GKSendDataReliable, 网络数据发送有误的时候,可以保证消息按照发送的顺序到达
GKSendDataUnreliable 只发一次
*/
[self.seccion sendDataToAllPeers:data withDataMode:GKSendDataReliable error:nil]; // first deprecated in iOS 7.0
} 

#pragma 相册的代理 
- (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.设置session会话,Handler(句柄,类似代理)
  [session setDataReceiveHandler:self withContext:nil];   // 2.控制器的移除
  [picker dismiss];
} // 从setDataReceiveHandler里面找出来
- (void)receiveData:(NSData *)data formPeer:(NSString *)peer inSession:(GKSession *)session context:(void *)context {   UIImage *image = [UIImage imageWithData:data];
  _imgView.image = image;
}

最新文章

  1. 数据结构与算法分析 java语音描述(引论)
  2. Oracle闪回技术详解
  3. Python之路,Day2 - Python基础2
  4. iptables防火墙作为基本需求的配置
  5. xcode6以后, 使用.pch
  6. java提高篇---LinkedList
  7. 复杂事件处理引擎—Esper入门
  8. execute immediate的简单用法(oracle)
  9. [LeetCode]题解(python):017-Letter Combinations of a Phone Number
  10. Git 开发新的功能分支
  11. snmp之GenericAddress
  12. Kinect v2(Microsoft Kinect for Windows v2 )配置移动电源解决方案
  13. 1.1 java语言有什么优点
  14. Python import this : The Zen of Python
  15. 界面渐变特效 -- CSS实现 -- 兼容IE8
  16. java实验-3
  17. Holt-Winters
  18. 05Hadoop 概论
  19. 力扣(LeetCode) 27. 移除元素
  20. idea环境下js、css中文乱码

热门文章

  1. Datatable paging,Repeater with Paging
  2. Drupal theme_hook
  3. JavaWeb_01_html基本学习
  4. HTML字符实体名称/实体编号
  5. html跨域获取数据
  6. java实现链表结构详细代码
  7. wxpython 创建工具栏和菜单栏
  8. Python 列表排序方法reverse、sort、sorted操作方法
  9. Linux--VSFTP服务搭建
  10. 如何删除Windows 10中的内存转储文件