iOS MultipeerConnectivity 编程发送图片服务器端

//

//  ViewController.m

//  070802blueserver

//

//  Created by tianshangrenjian on 15/7/8.

//  Copyright (c) 2015 tianshangrenjian. All rights reserved.

//


#import "ViewController.h"

#import <MultipeerConnectivity/MultipeerConnectivity.h>

@interface ViewController ()<MCSessionDelegate,UINavigationControllerDelegate,UIImagePickerControllerDelegate>

@property (weak, nonatomic) IBOutlet UIImageView *ImgView;

@property (strong,nonatomic) MCSession *session;

@property (strong, nonatomic) MCAdvertiserAssistant *assistant;

@property (strong, nonatomic) UIImagePickerController *imgPicker;


@end


@implementation ViewController


- (void)viewDidLoad {

    [super viewDidLoad];

    // Do any additional setup after loading the view, typically from a nib.

    MCPeerID *peer=[[MCPeerID alloc] initWithDisplayName:@"sch-server"];

    MCSession *session=[[MCSession alloc] initWithPeer:peer];

    session.delegate=self;

    self.session=session;

    

    

    MCAdvertiserAssistant *assistant=[[MCAdvertiserAssistant alloc] initWithServiceType:@"sch-server" discoveryInfo:nil session:session];

    

    self.assistant=assistant;

    

}



- (void)dealloc

{

    [self.assistant stop ];

}

- (void)didReceiveMemoryWarning {

    [super didReceiveMemoryWarning];

    // Dispose of any resources that can be recreated.

}


- (IBAction)btStart:(id)sender {

    [self.assistant start];

    

}


- (IBAction)btPic:(id)sender {

    

    UIImagePickerController *iPc=[[UIImagePickerController alloc] init];

    self.imgPicker=iPc;

    iPc.delegate=self;

    [self presentViewController:iPc animated:YES completion:nil];

    

}


- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info

{

    UIImage *img=info[UIImagePickerControllerOriginalImage];

    self.ImgView.image=img;

    NSData *data=UIImagePNGRepresentation(img);

    [self.session sendData:data toPeers:[self.session connectedPeers] withMode:MCSessionSendDataUnreliable error:nil];

    

    

    [picker dismissViewControllerAnimated:YES completion:nil];

}

- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker

{

    [picker dismissViewControllerAnimated:YES completion:nil];

}



// Remote peer changed state

- (void)session:(MCSession *)session peer:(MCPeerID *)peerID didChangeState:(MCSessionState)state

{

    switch (state) {

        case MCSessionStateConnecting:

            NSLog(@"connectiing!");

            break;

        case MCSessionStateConnected:

            NSLog(@"connected!");

        default:

            break;

    }


}


// Received data from remote peer

- (void)session:(MCSession *)session didReceiveData:(NSData *)data fromPeer:(MCPeerID *)peerID

{

    NSLog(@"receive data");

    UIImage *img=[UIImage imageWithData:data];

    self.ImgView.image=img;

}


// Received a byte stream from remote peer

- (void)session:(MCSession *)session didReceiveStream:(NSInputStream *)stream withName:(NSString *)streamName fromPeer:(MCPeerID *)peerID

{

    

}


// Start receiving a resource from remote peer

- (void)session:(MCSession *)session didStartReceivingResourceWithName:(NSString *)resourceName fromPeer:(MCPeerID *)peerID withProgress:(NSProgress *)progress

{

    

}


// Finished receiving a resource from remote peer and saved the content in a temporary location - the app is responsible for moving the file to a permanent location within its sandbox

- (void)session:(MCSession *)session didFinishReceivingResourceWithName:(NSString *)resourceName fromPeer:(MCPeerID *)peerID atURL:(NSURL *)localURL withError:(NSError *)error

{

    

}





@end


阅读更多
文章标签: ios 网络编程
个人分类: objective-c
上一篇iOS 网络编程 XMPP编程
下一篇iOS MultipeerConnectivity 编程发送图片客户端
想对作者说点什么? 我来说一句

没有更多推荐了,返回首页

关闭
关闭