这一篇博文讲述发微博界面的实现。

首先我们先了解一下在这个发微博界面中需要做哪些事情吧!

(1) 发微博包括文字内容和微博图片,所以我们可以用一个textview来装载微博文字内容,用一个imageview来装载图片内容。

①在文字部分,用一个textview,在发送的时候检测一下发送文字的个数,如果超过140,那么给出提示信息。在图片部分,用一个imageview,并且如果添加了图片,那么在图片的右上角添加一个打叉的按钮,作用是去除图片;当然,在你没有选择添加图片或者取消了已选图片时,按键自动取消。效果如下图:

这个打叉的cancelButton的处理比较简单,代码如下:

  1. - (IBAction)cancelImage:(id)sender {
  2. _cancelButton.hidden = YES;
  3. self.theImageView.image = [UIImage imageNamed:@"noImage50x118.png"];
  4. }

在导航栏的右边添加一个发送按键,处理相关的发送信息。代码如下:

  1. - (IBAction)sendWeibo:(id)sender {
  2. [self.theTextView resignFirstResponder];
  3. NSString *content = [[NSString alloc] initWithString:_theTextView.text];
  4. //计算发送微博的内容字数,并作相应的处理
  5. NSInteger contentLength = content.length;
  6. if (contentLength > 140) {
  7. MBProgressHUD *overLengthHud = [[MBProgressHUD alloc] initWithView:self.view];
  8. [self.view addSubview:overLengthHud];
  9. overLengthHud.mode = MBProgressHUDModeText;
  10. overLengthHud.labelText = @"提示信息";
  11. overLengthHud.detailsLabelText = [NSString stringWithFormat:@"微博字数:%d 超过140上限!",contentLength];
  12. [overLengthHud show:YES];
  13. [overLengthHud hide:YES afterDelay:2];
  14. }
  15. else {
  16. UIImage *image = _theImageView.image;
  17. //没有图片
  18. if (!hasPostImage) {
  19. [self postWithText:content];
  20. }
  21. //有图片
  22. else {
  23. [self postWithText:content image:image];
  24. }
  25. hud = [[MBProgressHUD alloc] init];
  26. hud.dimBackground = YES;
  27. hud.labelText = @"正在发送...";
  28. [hud show:YES];
  29. [self.view addSubview:hud];
  30. }
  31. }

注意到其中的方法 -(void) postWithText:(NSString*)text 是发单纯文字微博的,而方法 -(void)postWithText:(NSString *)text image:(UIImage*)image 是发文字+图片微博的。具体的实现过程下面会讲解。

修改:突然发现其实上面的发送代码中还存在一个小问题,假如其中不输入文字也可以发送,显然这是不对的。那么还有添加一个if判断一下字数长度是否为0,如果是0的话给出一个alert窗口提示一下。

  1. if (contentLength == 0) {
  2. UIAlertView *alert = [[UIAlertView alloc] initWithTitle:nil message:@"请输入微博内容!" delegate:nil cancelButtonTitle:@"好" otherButtonTitles:nil, nil];
  3. [alert show];
  4. }
  5. else if (contentLength > 140) {
  6. }
  7. else {
  8. }

②这里要着重说的是如何添加图片。

这里用一个按键用于插入图片。

插入的图片来源包括系统相册和拍摄。

在这里我们需要用到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;

在这里我们需要实现下面的这个第一个方法,这样当选取一个图像时,委托类就会得到通知,在这个方法中添加图像处理的有关代码就可以实现对选取图片的处理。

添加图片这部分的代码如下:

  1. - (IBAction)addPhoto:(id)sender {
  2. UIAlertView *alert = [[UIAlertView alloc] initWithTitle:nil message:@"插入图片" delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"系统相册",@"拍摄", nil];
  3. [alert show];
  4. }
  5. #pragma mark - UIAlertViewDelegate
  6. - (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
  7. {
  8. if (buttonIndex == 1)
  9. {
  10. [self addPhoto];
  11. }
  12. else if(buttonIndex == 2)
  13. {
  14. [self takePhoto];
  15. }
  16. }
  17. - (void)addPhoto
  18. {
  19. UIImagePickerController * imagePickerController = [[UIImagePickerController alloc]init];
  20. imagePickerController.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
  21. imagePickerController.delegate = self;
  22. imagePickerController.allowsEditing = NO;
  23. [self presentModalViewController:imagePickerController animated:YES];
  24. }
  25. - (void)takePhoto
  26. {
  27. if (![UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera])
  28. {
  29. UIAlertView *alert = [[UIAlertView alloc] initWithTitle:nil
  30. message:@"该设备不支持拍照功能"
  31. delegate:nil
  32. cancelButtonTitle:nil
  33. otherButtonTitles:@"好", nil];
  34. [alert show];
  35. }
  36. else
  37. {
  38. UIImagePickerController * imagePickerController = [[UIImagePickerController alloc]init];
  39. imagePickerController.sourceType = UIImagePickerControllerSourceTypeCamera;
  40. imagePickerController.delegate = self;
  41. imagePickerController.allowsEditing = NO;
  42. [self presentModalViewController:imagePickerController animated:YES];
  43. }
  44. }
  45. #pragma mark - UIImagePickerControllerDelegate
  46. - (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
  47. {
  48. [picker dismissModalViewControllerAnimated:YES];
  49. UIImage * image = [info objectForKey:@"UIImagePickerControllerOriginalImage"];
  50. self.theImageView.image = image;
  51. hasPostImage = YES;
  52. _cancelButton.hidden = NO;
  53. }

(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这个第三方类库。

处理的代码如下:

  1. //发布文字微博
  2. -(void) postWithText:(NSString*)text {
  3. NSURL *url = [NSURL URLWithString:WEIBO_UPDATE];
  4. ASIFormDataRequest *item = [[ASIFormDataRequest alloc] initWithURL:url];
  5. [item setPostValue:[InfoForSina returnAccessTokenString]    forKey:@"access_token"];
  6. [item setPostValue:text                                     forKey:@"status"];
  7. [item setCompletionBlock:^{
  8. self.theTextView.text = nil;
  9. [hud removeFromSuperview];
  10. MBProgressHUD *custuonHUD = [[MBProgressHUD alloc]initWithView:self.view];
  11. custuonHUD.customView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"37x-Checkmark.png"]];
  12. custuonHUD.labelText = @"发微博成功!";
  13. custuonHUD.mode = MBProgressHUDModeCustomView;
  14. [self.view addSubview:custuonHUD];
  15. [custuonHUD show:YES];
  16. [custuonHUD hide:YES afterDelay:1];
  17. }];
  18. [item startAsynchronous];
  19. }
  20. //发布文字图片微博
  21. -(void)postWithText:(NSString *)text image:(UIImage*)image {
  22. NSURL *url = [NSURL URLWithString:WEIBO_UPLOAD];
  23. ASIFormDataRequest *item = [[ASIFormDataRequest alloc] initWithURL:url];
  24. [item setPostValue:[InfoForSina returnAccessTokenString]    forKey:@"access_token"];
  25. [item setPostValue:text                                     forKey:@"status"];
  26. [item addData:UIImagePNGRepresentation(image)               forKey:@"pic"];
  27. [item setCompletionBlock:^{
  28. self.theImageView.image = [UIImage imageNamed:@"noImage50x118.png"];
  29. self.theTextView.text = nil;
  30. [hud removeFromSuperview];
  31. _cancelButton.hidden = NO;
  32. MBProgressHUD *custuonHUD = [[MBProgressHUD alloc]initWithView:self.view];
  33. custuonHUD.customView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"37x-Checkmark.png"]];
  34. custuonHUD.labelText = @"发微博成功!";
  35. custuonHUD.mode = MBProgressHUDModeCustomView;
  36. [self.view addSubview:custuonHUD];
  37. [custuonHUD show:YES];
  38. [custuonHUD hide:YES afterDelay:1];
  39. }];
  40. [item startAsynchronous];
  41. }

使用时需要#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。效果图如下。

好了,大概就这样说完了!

ios sinaweibo 客户端(二)的更多相关文章

  1. ios sinaweibo 客户端(三)

    这个页面要讲述的是用户的粉丝列表,下面是效果图: 可以看到这个视图明显也是一个tableview,在每一个cell中包含的有三个部分的内容:粉丝头像image,粉丝昵称label,我和粉丝之间的相互关 ...

  2. ios sinaweibo 客户端(一)

    上一篇sina微博Demo已经完成的认证,下面就开始进入微博相关内容的加载及显示.其实主要的工作就是调用微博API 加载相关的json数据,然后进行解析,然后在界面中进行组织好在tableview中进 ...

  3. 微信连WiFi关注公众号流程更新 解决ios微信扫描二维码不关注就能上网的问题

    前几天鼓捣了一下微信连WiFi功能,设置还蛮简单的,但ytkah发现如果是ios版微信扫描微信连WiFi生成的二维码不用关注公众号就可以直接上网了,而安卓版需要关注公众号才能上网,这样就少了很多ios ...

  4. XMPPFrameWork IOS 开发(二)- xcode配置

    原始地址:XMPPFrameWork IOS 开发(二) 译文地址:   Getting started using XMPPFramework on iOS 介绍 ios上的XMPPFramewor ...

  5. 一元云购完整源码 云购CMS系统 带安卓和ios手机客户端

    看起来不错的一套一元云购CMS源码,源码包里面带了安卓和ios手机客户端,手机客户端需要自己反编译.    这里不做功能和其它更多的介绍,可以自己下载后慢慢测试了解.    下面演示图为亲测截图< ...

  6. 2016最新一元云购完整源码 云购CMS系统 带安卓和ios手机客户端 源码免费分享

    原文转自:http://www.zccode.com/thread-724-1-1.html 该资源说明: 看起来不错的一套一元云购CMS源码,源码包里面带了安卓和ios手机客户端,手机客户端需要自己 ...

  7. iOS runtime探究(二): 从runtime開始深入理解OC消息转发机制

    你要知道的runtime都在这里 转载请注明出处 http://blog.csdn.net/u014205968/article/details/67639289 本文主要解说runtime相关知识, ...

  8. 苹果IOS内购二次验证返回state为21002的坑

    项目是三四年前的老项目,之前有IOS内购二次验证的接口,貌似很久都没用了,然而最近IOS的妹子说接口用不了,让我看看啥问题.接口流程时很简单的,就是前端IOS在购买成功之后,接收到receipt后进行 ...

  9. 在ios微信客户端遇到的坑,input等错位

    1.判断移动端设备 // 处理iOS 微信客户端弹窗状态下调起输入法后关闭输入法页面元素错位解决办法 var ua = window.navigator.userAgent.toLowerCase() ...

随机推荐

  1. vector详解

    /*vector向量容器*/ //用数组方式访问vector元素 #include<iostream> #include<vector> #include<cstdio& ...

  2. 能量项链 洛谷P1063

    1154 能量项链 2006年NOIP全国联赛提高组 时间限制: 1 s 空间限制: 128000 KB 题目等级 : 黄金 Gold 题解 题目描述 Description 在Mars星球上,每个M ...

  3. [Xcode 实际操作]六、媒体与动画-(10)UIView视图翻转动的画制作

    目录:[Swift]Xcode实际操作 本文将演示翻转动画的制作. 在项目导航区,打开视图控制器的代码文件[ViewController.swift] import UIKit class ViewC ...

  4. IT兄弟连 JavaWeb教程 过滤器1

    Servlet过滤器是从Servlet2.3规范开始新增的功能,并在Servlet2.4规范中得到增强,监听器可以监听到Web应用程序启动和关闭.创建过滤器和监听器需要继承相应接口,并对其进行配置. ...

  5. 黑马旅游网配置 pom.xml

    <?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://mave ...

  6. Java基础笔记(二)——配置环境变量

    https://www.oracle.com/technetwork/java/javase/downloads/jdk8-downloads-2133151.html 到此处下载jdk,并安装.(选 ...

  7. C# 数组之int[]

    一.数组分类 数组可以简单分为3类: 1维数组 2维数组 交错数组 二.数组初始化 1.一维数组 int [] A = { 1,2,3,4 } 直接赋值 或者 int [] A = new int [ ...

  8. 自定义view(14)使用Path绘制复杂图形

    灵活使用 Path ,可以画出复杂图形,就像美术生在画板上画复杂图形一样.程序员也可以用代码实现. 1.样板图片 这个是个温度计,它是静态的,温度值是动态变化的,所以要自定义个view.动态显示值,温 ...

  9. aix OPATH ISSUE

    issue 1: OPatch cannot find a valid oraInst.loc file to locate Central Inventory (OPatch failed with ...

  10. 大都市 meg

    Description 在经济全球化浪潮的影响下,习惯于漫步在清晨的乡间小路的邮递员Blue Mary也开始骑着摩托车传递邮件了.不过,她经常回忆起以前在乡间漫步的情景. 昔日,乡下有依次编号为1.. ...