ios sinaweibo 客户端(二)
这一篇博文讲述发微博界面的实现。
首先我们先了解一下在这个发微博界面中需要做哪些事情吧!
(1) 发微博包括文字内容和微博图片,所以我们可以用一个textview来装载微博文字内容,用一个imageview来装载图片内容。
①在文字部分,用一个textview,在发送的时候检测一下发送文字的个数,如果超过140,那么给出提示信息。在图片部分,用一个imageview,并且如果添加了图片,那么在图片的右上角添加一个打叉的按钮,作用是去除图片;当然,在你没有选择添加图片或者取消了已选图片时,按键自动取消。效果如下图:
这个打叉的cancelButton的处理比较简单,代码如下:
- - (IBAction)cancelImage:(id)sender {
- _cancelButton.hidden = YES;
- self.theImageView.image = [UIImage imageNamed:@"noImage50x118.png"];
- }
在导航栏的右边添加一个发送按键,处理相关的发送信息。代码如下:
- - (IBAction)sendWeibo:(id)sender {
- [self.theTextView resignFirstResponder];
- NSString *content = [[NSString alloc] initWithString:_theTextView.text];
- //计算发送微博的内容字数,并作相应的处理
- NSInteger contentLength = content.length;
- if (contentLength > 140) {
- MBProgressHUD *overLengthHud = [[MBProgressHUD alloc] initWithView:self.view];
- [self.view addSubview:overLengthHud];
- overLengthHud.mode = MBProgressHUDModeText;
- overLengthHud.labelText = @"提示信息";
- overLengthHud.detailsLabelText = [NSString stringWithFormat:@"微博字数:%d 超过140上限!",contentLength];
- [overLengthHud show:YES];
- [overLengthHud hide:YES afterDelay:2];
- }
- else {
- UIImage *image = _theImageView.image;
- //没有图片
- if (!hasPostImage) {
- [self postWithText:content];
- }
- //有图片
- else {
- [self postWithText:content image:image];
- }
- hud = [[MBProgressHUD alloc] init];
- hud.dimBackground = YES;
- hud.labelText = @"正在发送...";
- [hud show:YES];
- [self.view addSubview:hud];
- }
- }
注意到其中的方法 -(void) postWithText:(NSString*)text 是发单纯文字微博的,而方法 -(void)postWithText:(NSString *)text image:(UIImage*)image 是发文字+图片微博的。具体的实现过程下面会讲解。
修改:突然发现其实上面的发送代码中还存在一个小问题,假如其中不输入文字也可以发送,显然这是不对的。那么还有添加一个if判断一下字数长度是否为0,如果是0的话给出一个alert窗口提示一下。
- if (contentLength == 0) {
- UIAlertView *alert = [[UIAlertView alloc] initWithTitle:nil message:@"请输入微博内容!" delegate:nil cancelButtonTitle:@"好" otherButtonTitles:nil, nil];
- [alert show];
- }
- else if (contentLength > 140) {
- }
- else {
- }
②这里要着重说的是如何添加图片。
这里用一个按键用于插入图片。
插入的图片来源包括系统相册和拍摄。
在这里我们需要用到UIImagePickerController 类来创建图像选取器。UIImagePickerController 图像选取器是一种导航控制器类,让你可以在应用程序中添加简单的图像选择功能或者照相机界面。用户会看到一个图像选择屏幕,在其中挑选相片,相片的来源则是他自己的相片库、保存下来的相片集或者照相机。当用户选定一个相片后,就会通过 UIImagePickerDelegate 协议中的方法,通知选取器的委托方法实现图像的选取。
The role and appearance of an image picker controller depend on the source type you assign to it before you present it.
A sourceType of UIImagePickerControllerSourceTypeCamera provides a user interface for taking a new picture or movie (on devices that support media capture).
A sourceType of UIImagePickerControllerSourceTypePhotoLibrary or UIImagePickerControllerSourceTypeSavedPhotosAlbum provides a user interface for choosing among saved pictures and movies.
这里说明了通过sourceType 设定图像的来源。包括
enum{
UIImagePickerControllerSourceTypePhotoLibrary,//相片库
UIImagePickerControllerSourceTypeCamera,//照相机
UIImagePickerControllerSourceTypeSavedPhotosAlbum//保存的相片
};
typedef NSUInteger UIImagePickerControllerSourceType;
Verify that the device is capable of picking content from the desired source. Do this calling the isSourceTypeAvailable: class method, providing a constant from the “UIImagePickerControllerSourceType” enumeration.
在使用时应先检查当前设备是否支持使用UIImagePickerController,这个时候我们需要调用isSourceTypeAvailable:方法判断,需要提供sourceType 作为参数
当用户选择一个图片之后,选择器的委托会通过 didFinishPickingImage 方法接到通知。代理会得到一个包含有该图像的 UIImage 对象,如果编辑功能开启的话,还会得到一个包含了编辑属性的NSDictionary。
使用UIImagePickerController的时候注意添加这两个delegate,<UIImagePickerControllerDelegate,UINavigationControllerDelegate>并实现其中的两个方法。
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker
设置选取器的delegate ,就可以将一个委托赋予选择器:picker.delegate =self;
在这里我们需要实现下面的这个第一个方法,这样当选取一个图像时,委托类就会得到通知,在这个方法中添加图像处理的有关代码就可以实现对选取图片的处理。
添加图片这部分的代码如下:
- - (IBAction)addPhoto:(id)sender {
- UIAlertView *alert = [[UIAlertView alloc] initWithTitle:nil message:@"插入图片" delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"系统相册",@"拍摄", nil];
- [alert show];
- }
- #pragma mark - UIAlertViewDelegate
- - (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
- {
- if (buttonIndex == 1)
- {
- [self addPhoto];
- }
- else if(buttonIndex == 2)
- {
- [self takePhoto];
- }
- }
- - (void)addPhoto
- {
- UIImagePickerController * imagePickerController = [[UIImagePickerController alloc]init];
- imagePickerController.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
- imagePickerController.delegate = self;
- imagePickerController.allowsEditing = NO;
- [self presentModalViewController:imagePickerController animated:YES];
- }
- - (void)takePhoto
- {
- if (![UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera])
- {
- UIAlertView *alert = [[UIAlertView alloc] initWithTitle:nil
- message:@"该设备不支持拍照功能"
- delegate:nil
- cancelButtonTitle:nil
- otherButtonTitles:@"好", nil];
- [alert show];
- }
- else
- {
- UIImagePickerController * imagePickerController = [[UIImagePickerController alloc]init];
- imagePickerController.sourceType = UIImagePickerControllerSourceTypeCamera;
- imagePickerController.delegate = self;
- imagePickerController.allowsEditing = NO;
- [self presentModalViewController:imagePickerController animated:YES];
- }
- }
- #pragma mark - UIImagePickerControllerDelegate
- - (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
- {
- [picker dismissModalViewControllerAnimated:YES];
- UIImage * image = [info objectForKey:@"UIImagePickerControllerOriginalImage"];
- self.theImageView.image = image;
- hasPostImage = YES;
- _cancelButton.hidden = NO;
- }
(1) 发微博需要用到的API调用;如果只是发文字微博的话,是用到这个API:https://api.weibo.com/2/statuses/update.json;如果是发文字+图片微博的话,用到的是这个API:https://upload.api.weibo.com/2/statuses/upload.json。注意到二者的HTTP请求都是POST;而且请求参数中也要做一点出来,文字的话必须做URLencode,内容不超过140个汉字;图片的话,需要是binary类型的,而且仅支持JPEG 、GIF 、PNG格式,图片大小小于5M。
这部分数据请求的处理我用到了ASIHTTPRequest这个第三方类库。
处理的代码如下:
- //发布文字微博
- -(void) postWithText:(NSString*)text {
- NSURL *url = [NSURL URLWithString:WEIBO_UPDATE];
- ASIFormDataRequest *item = [[ASIFormDataRequest alloc] initWithURL:url];
- [item setPostValue:[InfoForSina returnAccessTokenString] forKey:@"access_token"];
- [item setPostValue:text forKey:@"status"];
- [item setCompletionBlock:^{
- self.theTextView.text = nil;
- [hud removeFromSuperview];
- MBProgressHUD *custuonHUD = [[MBProgressHUD alloc]initWithView:self.view];
- custuonHUD.customView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"37x-Checkmark.png"]];
- custuonHUD.labelText = @"发微博成功!";
- custuonHUD.mode = MBProgressHUDModeCustomView;
- [self.view addSubview:custuonHUD];
- [custuonHUD show:YES];
- [custuonHUD hide:YES afterDelay:1];
- }];
- [item startAsynchronous];
- }
- //发布文字图片微博
- -(void)postWithText:(NSString *)text image:(UIImage*)image {
- NSURL *url = [NSURL URLWithString:WEIBO_UPLOAD];
- ASIFormDataRequest *item = [[ASIFormDataRequest alloc] initWithURL:url];
- [item setPostValue:[InfoForSina returnAccessTokenString] forKey:@"access_token"];
- [item setPostValue:text forKey:@"status"];
- [item addData:UIImagePNGRepresentation(image) forKey:@"pic"];
- [item setCompletionBlock:^{
- self.theImageView.image = [UIImage imageNamed:@"noImage50x118.png"];
- self.theTextView.text = nil;
- [hud removeFromSuperview];
- _cancelButton.hidden = NO;
- MBProgressHUD *custuonHUD = [[MBProgressHUD alloc]initWithView:self.view];
- custuonHUD.customView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"37x-Checkmark.png"]];
- custuonHUD.labelText = @"发微博成功!";
- custuonHUD.mode = MBProgressHUDModeCustomView;
- [self.view addSubview:custuonHUD];
- [custuonHUD show:YES];
- [custuonHUD hide:YES afterDelay:1];
- }];
- [item startAsynchronous];
- }
使用时需要#import "ASIFormDataRequest.h"
有两点需要说明的是:
1、前面已经说到文字内容需要URLencode,在这里我么并没有做什么处理,因为我们使用的这个第三方类库已经帮我们实现了;对于微博图片内容,必须转换成binary现实,我们是这样处理的:UIImagePNGRepresentation(image)
NSData * UIImagePNGRepresentation (
UIImage *image
);
Returns the data for the specified image in PNG format
2、在发送完成的block中我们对textview和imagview都做了处理,方便发送下一条微博;同时我添加了一个提示框提示发送成功的信息,在这个提示框中我使用了customView,也就是打钩的image。效果图如下。
好了,大概就这样说完了!
转载于:https://www.cnblogs.com/yulang314/p/3549547.html
ios sinaweibo 客户端(二)相关推荐
- ios sinaweibo 客户端(三)
这个页面要讲述的是用户的粉丝列表,下面是效果图: 可以看到这个视图明显也是一个tableview,在每一个cell中包含的有三个部分的内容:粉丝头像image,粉丝昵称label,我和粉丝之间的相互关 ...
- ios sinaweibo 客户端(一)
上一篇sina微博Demo已经完成的认证,下面就开始进入微博相关内容的加载及显示.其实主要的工作就是调用微博API 加载相关的json数据,然后进行解析,然后在界面中进行组织好在tableview中进 ...
- 除草机(Grasscutter) ios/Android客户端配置教程
本文章仅为Grasscutter的ios/Android客户端配置教程,不提供任何服务器,如果你按图中的数据连上了,纯属瞎猫碰到死耗子. 对应的客户端配置教程在:从零开始,一镜到底,纯净系统搭建除草机 ...
- 基于XMPP的IOS聊天客户端程序(XMPP服务器架构)
最近看了关于XMPP的框架,以文本聊天为例,需要发送的消息为: <message type="chat" from="kang@server.com" t ...
- [iPhone高级] 基于XMPP的IOS聊天客户端程序(XMPP服务器架构)
最近看了关于XMPP的框架,以文本聊天为例,需要发送的消息为: [html] view plaincopy <message type="chat" from="k ...
- iOS原生实现二维码扫描
iOS原生实现二维码扫描 最近项目上需要开发扫描二维码进行签到的功能,主要用于开会签到的场景,所以为了避免作弊,我们再开发时只采用直接扫描的方式,并且要屏蔽从相册读取图片,此外还在二维码扫描成功签到时 ...
- iOS 苹果手机客户端微信支付调起失败--无法调起微信的原因
1. iOS 苹果手机客户端微信支付调起失败–无法调起微信的原因 微信的SDK分两种,一种包含支付的SDK,另一中是不包含支付的SDK, 因为项目早期集成的是微信分享的SDK,不带支付的功能,所以更换 ...
- android iOS App客户端如何实现在线支付
*android iOS App客户端如何实现在线支付**Q~~1⑨9⑦O*⑦46 阔别已久,小课堂再次开课,今天将和大家分享在开通各个支付渠道之前,你可能想要了解的一些信息. 对支付领域一无所知,对 ...
- ios内购二次验证安全性问题_苹果IOS内购二次验证返回state为21002的坑
项目是三四年前的老项目,之前有IOS内购二次验证的接口,貌似很久都没用了,然而最近IOS的妹子说接口用不了,让我看看啥问题.接口流程时很简单的,就是前端IOS在购买成功之后,接收到receipt后进行 ...
- iOS相册图片二维码识别
前言:最近客户要求开发一个功能,类似微信长按图片识别图片中的二维码,一开始我使用了ZXingObjC,但是完成后被测试出有些二维码识别不了,所以只能另寻它法,之后更换为苹果系统自带的识别图片二维码的功 ...
最新文章
- Swift -布局框架SnapKit使用
- Nature癌症“牵线木偶”理论:科学家找到了不易误伤健康细胞的“剪刀”
- idea集成python_IDEA集成Python插件,SDK配置
- Spring MVC与Struts2对比
- memcached php封装类,PHP Memcached + APC + 文件缓存封装_PHP - key
- 用于制作app store的截图的工具:Brief Wrapper —— 最便捷的应用商店屏幕快照
- 硬盘整数分区计算方法一般算法
- PMP项目管理13个计划
- Origin软件使用TIPS
- 联想E431 win10+Unbutun+Deepin系统安装
- 9、recoil库的基本使用
- IM群聊消息的已读未读功能在存储空间方面的实现思路探讨
- 高等数学——积分中值定理
- response导出html到word
- 西安思源中学2021高考成绩查询入口,2021年西安各高中高考成绩排名及放榜最新消息...
- IDEA中搜索 matches limit 默认显示100,调整限制,增加搜索返回数量
- 工程伦理 第三章习题 答案
- jsp+ssm计算机毕业设计游戏装备交易网站论文2022【附源码】
- 国家开放大学-乡村社会学-形考作业2
- mips汇编代码示例解释_通过示例解释cosmosdb