使用iOS SDK中的HTTP网络请求API,相当的复杂,调用很繁琐,ASIHTTPRequest就是一个对CFNetwork API进行了封装,并且使用起来非常简单的一套API,用Objective-C编写,可以很好的应用在Mac OS X系统和iOS平台的应用程序中。ASIHTTPRequest适用于基本的HTTP请求,和基于REST的服务之间的交互。
2.ASIHTTPRequest功能很强大,主要特色如下:
- l 通过简单的接口,即可完成向服务端提交数据和从服务端获取数据的工作
- l 下载的数据,可存储到内存中或直接存储到磁盘中
- l 能上传本地文件到服务端
- l 可以方便的访问和操作请求和返回的Http头信息
- l 可以获取到上传或下载的进度信息,为应用程序提供更好的体验
- l 支持上传或下载队列,并且可获取队列的进度信息
- l 支持基本、摘要和NTLM身份认证,在同一会话中授权凭证会自动维持,并且可以存储在Keychain(Mac和iOS操作系统的密码管理系统)中
- l 支持Cookie
- l 当应用(iOS 4+)在后台运行时,请求可以继续运行
- l 支持GZIP压缩数据
- l 内置的ASIDownloadCache类,可以缓存请求返回的数据,这样即使没有网络也可以返回已经缓存的数据结果
- l ASIWebPageRequest –可以下载完整的网页,包括包含的网页、样式表、脚本等资源文件,并显示在UIWebView /WebView中。任意大小的页面都可以无限期缓存,这样即使没有网络也可以离线浏览
- l 支持客户端证书
- l 支持通过代理发起Http请求
- l 支持带宽限制。在iOS平台,可以根据当前网络情况来自动决定是否限制带宽,例如当使用WWAN(GPRS/Edge/3G)网络时限制,而当使用WIFI时不做任何限制
- l 支持断点续传
- l 支持同步和异步请求
在源代码的class文件夹中,copy下面的这些文件到你的ios工程中,如果你不确定那个文件是你需要的,你最好全部拷贝到你的工程中
- ASIHTTPRequestConfig.h
- ASIHTTPRequestDelegate.h
- ASIProgressDelegate.h
- ASICacheDelegate.h
- ASIHTTPRequest.h
- ASIHTTPRequest.m
- ASIDataCompressor.h
- ASIDataCompressor.m
- ASIDataDecompressor.h
- ASIDataDecompressor.m
- ASIFormDataRequest.h
- ASIInputStream.h
- ASIInputStream.m
- ASIFormDataRequest.m
- ASINetworkQueue.h
- ASINetworkQueue.m
- ASIDownloadCache.h
- ASIDownloadCache.m
iPhone projects must also include:
- ASIAuthenticationDialog.h
- ASIAuthenticationDialog.m
- Reachability.h (in the External/Reachability folder)
- Reachability.m (in the External/Reachability folder)
2.添加依赖包CFNetwork, SystemConfiguration, MobileCoreServices, CoreGraphics and zlib
打开Build Phases tab,展开 Link Binary With Libraries然后点击 + 按钮
选中 CFNetwork.framework ,然后添加进来
重复上述步骤,依次添加:: SystemConfiguration.framework, MobileCoreServices.framework,CoreGraphics.framework and libz.dylib.
4.如何使用相应的API
由于篇幅有限,我只在这里简单介绍同步和异步的方法实现,具体文档详见官方地址如下:http://allseeing-i.com/ASIHTTPRequest/,
1)创建的同步请求:
-(IBAction)grabURL:(id)sender{NSURL*url =[NSURL URLWithString:@"http://allseeing-i.com"]; ASIHTTPRequest*request =[ASIHTTPRequest requestWithURL:url]; [request startSynchronous]; NSError*error =[request error]; if(!error){NSString*response =[request responseString]; }}
2)同步请求在实际应用中很少用到,因为同步的话,实际是执行的主线程,如果网络很慢或请求的数据很大,前台界面会一片空白,所以这时候我们往往会采用异步请求数据:
-(IBAction)grabURLInBackground:(id)sender {NSURL*url =[NSURL URLWithString:@"http://allseeing-i.com"]; ASIHTTPRequest*request =[ASIHTTPRequest requestWithURL:url]; [request setDelegate:self]; [request startAsynchronous]; } -(void)requestFinished:(ASIHTTPRequest*)request {// Use when fetching text dataNSString*responseString =[request responseString]; // Use when fetching binary dataNSData*responseData =[request responseData]; } -(void)requestFailed:(ASIHTTPRequest*)request {NSError*error =[request error]; }
3)新版本支持的block
-(IBAction)grabURLInBackground:(id)sender {NSURL*url =[NSURL URLWithString:@"http://allseeing-i.com"]; __block ASIHTTPRequest*request =[ASIHTTPRequest requestWithURL:url]; [request setCompletionBlock:^{// Use when fetching text dataNSString*responseString =[request responseString]; // Use when fetching binary dataNSData*responseData =[request responseData]; }]; [request setFailedBlock:^{NSError*error =[request error]; }]; [request startAsynchronous]; }
Note the use of the __block qualifier when we declare the request, this is important! It tells the block not to retain the request, which is important in preventing a retain-cycle, since the request will always retain the block.
You need to load it using the -loadNibNamed method. -initWithNibName is only for UIViewControllers.
Add the following code to your MyCustomView init method:
NSArray *subviewArray = [[NSBundle mainBundle] loadNibNamed:@"MyCustomView" owner:self options:nil]; UIView *mainView = [subviewArray objectAtIndex:0]; [self addSubview:mainView];
Remember, if you are initializing an object from a nib, it calls - (id)initWithCoder:(NSCoder *)aDecoder to initialize, so you'll have to override that if you are creating the MyCustomView object within the nib. If you're just doing it with initWithFrame:, then just override that and add the code above. Also, in your nib, make sure you have one top-level UIView, and place all other elements within that (that makes sure that your subviewArray only has one entry).
This will load the views from the nib and add them to the object, and should do the trick.
Conversation
------------------------------------------------------
I used your code in the initWithFrame and now I can see the MyCustomView with all its elements, but there is one thing that doesn't work. In the viewDidLoad of MyViewController I've: UIView *subView = [[MyCustomView alloc] initWithFrame:myTableView.frame]; [subView setBackgroundColor:[UIColor colorWithRed:0.0 green:0.0 blue:0.0 alpha:0.5]]; [myTableView addSubview:subView]; [subView release]; so that I should see the table under it. Any ideas? – Cricket
------------------------------------------------------------------------------------------
It works, if I set it in the initWithFrame. Now I've to understand how support the landscape mode for this view :) Thanks
------------------------------------------------------------------------------------------
I've found another solution that resolves also the landscape problem. I use the loadNibNamed code in MyViewController (when I have to display the subview) and I put any other customization in the awakeFromNib method in MyCustomView.m –
------------------------------------------------------------------------------------------
Hey, the method [self addSubview:mainView] only adds the view you made in IB to the MyCustomView. I tried your method and then tried to set the frame for MyCustomView. Turns out that the frame changes but the frame of the view made in .xib does not change. How do i change the frame of the IB view through MyCustomView? – HG's
------------------------------------------------------------------------------------------
Just do mainView.frame = newFrame. So, if you want the IB view's frame to take up the entire MyCustomView frame, do "mainView.frame = self.bounds". – Ned
------------------------------------------------------------------------------------------
libgdx自0.9.8版本开始对tile map的读取重新进行了实现,原来com.badlogic.gdx.graphics.g2d.tiled包下的类不在使用,新的map包位于com.badlogic.gdx.maps,增强了libgdx在地图方面的扩展性、灵活性。前后两种tile map的加载方式也有所区别。0.9.8之前的tiledmap加载方法请见博文:http://www.cnblogs.com/htynkn/archive/2012/01/13/libgdx_14.html
一、map类结构关系
代码见这里
可以继承map、maprenderer等实现自己的地图结构
二、tmx文件结构
<?xml version="1.0" encoding="UTF-8"?> <map version="1.0" orientation="orthogonal" width="13" height="8" tilewidth="64" tileheight="64"> <tileset firstgid="1" name="grass_ground" tilewidth="64" tileheight="64"> <image source="grass_ground.png" trans="000000" width="512" height="512"/> <tile id="56"> <properties> <property name="groupid" value="1"/> </properties> </tile> <tile id="57"> <properties> <property name="groupid" value="2"/> </properties> </tile> <tile id="60"> <properties> <property name="groupid" value="1"/> <property name="targetid" value="2"/> <property name="unittype" value="hero"/> </properties> </tile> <tile id="61"> <properties> <property name="groupid" value="2"/> <property name="targetid" value="1"/> <property name="unittype" value="hero"/> </properties> </tile> </tileset> <tileset firstgid="65" name="201201121554224250" tilewidth="64" tileheight="64"> <image source="201201121554224250.png" width="321" height="242"/> </tileset> <layer name="ground" width="13" height="8"> <data encoding="base64" compression="zlib"> eJxd0NsKwkAMRdEB672KivggImpVBK2XVlv//8vcgRMIPiySyXQS0nVKKZcRtliF81C5xTE26KIO7ugpf2OPo3KrfdDHK3xT4qLcFBiEnoXe1Hq3Q4aTzuaBKyppQo8DZpj8zXaVYhPm2B4LTMOO3tvmn8M+fv/Uf8rDPr7jTTWfs0SLLzqKbahl4ndz/AA4bQ5u </data> </layer> <layer name="units" width="13" height="8"> <data encoding="base64" compression="zlib"> eJxjYCAdWJKo3ooCPbZEqrej0B5S9dALAACEqgK7 </data> </layer> <objectgroup name="player" width="13" height="8"/> <objectgroup name="123" width="13" height="8"> <object name="123" x="125" y="209" width="79" height="61"/> <object name="235" x="320" y="211"> <polyline points="0,0 64,26 45,39 0,45 -3,0 8,26 -2,36"/> </object> <object name="568" x="400" y="286" width="58" height="52"> <ellipse/> </object> <object name="123123" x="433" y="172"> <polyline points="0,0 63,31 8,50 -1,-2"/> </object> <object name="sdf" x="526" y="270"> <polygon points="0,0 -3,99 39,62 62,14 49,92"/> </object> <object name="wersf" gid="70" x="338" y="346"/> </objectgroup> </map>
tileset是Tile编辑器中的图块,一个图块是一个tileset,当地图中使用到一个tile时,会将该tile的数据加入到tileset中,每一个tile有一个id,多个图块的tile id是递增的。
layer是Tile编辑器中的图层,一个地图有多个图层,一个图层是一个layer,图层中存放tile数据,并使用了编码和压缩格式,encoding,compression属性;
objectgroup是Tile编辑器中的对象,一个objectgroup可以多个object,object有多种类型:polyline、ellipse、polygon等等,它们有不同的属性。
三、TiledMap类结构
com.badlogic.gdx.maps.tiled 包下类结构
TiledMap:继承Map类,表示整个tiledmap对象;
TiledMapTileSet:对应tmx文件中的图块,一个图块为一个TiledMapTileSet;
TiledMapTile:对应图块中的一个块,多个TileMapTile组成一个TiledMapTileSet;
TiledMapTileLayer:继承MayLayer类 对应tmx文件中的一个图层;
TiledMapTileSets:一个map中用到的所有TiledMapTileSet;
下面准备写一下TmxMapLoader和TiledMapRenderer
参考资料:https://code.google.com/p/libgdx/wiki/GraphicsTileMaps