Bmob 提取图片(网络图片)
2015-09-17 21:45 真实16 阅读(626) 评论(0) 编辑 收藏 举报#import "ViewController.h"
#import <BmobSDK/Bmob.h>
@interface ViewController ()
{
NSMutableDictionary *dic;
}
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
dic = [NSMutableDictionary dictionary];
/*
--------------在bmob 添加一组数据
//往GameScore表添加一条playerName为小明,分数为78的数据
BmobObject *gameScore = [BmobObject objectWithClassName:@"zhenShi"];
[gameScore setObject:@"真实" forKey:@"wukun"];
[gameScore setObject:@100 forKey:@"ww"];
[gameScore setObject:[NSNumber numberWithBool:YES] forKey:@"cheatMode"];
[gameScore saveInBackgroundWithResultBlock:^(BOOL isSuccessful, NSError *error) {
//进行操作
}];
-----------查找数据
BmobQuery *bquery = [BmobQuery queryWithClassName:@"GameScore"];
//查找GameScore表的数据
[bquery findObjectsInBackgroundWithBlock:^(NSArray *array, NSError *error) {
for (BmobObject *obj in array) {
//打印playerName
NSLog(@"obj.playerName = %@", [obj objectForKey:@"playerName"]);
//打印objectId,createdAt,updatedAt
NSLog(@"obj.objectId = %@", [obj objectId]);
NSLog(@"obj.createdAt = %@", [obj createdAt]);
NSLog(@"obj.updatedAt = %@", [obj updatedAt]);
}
}];
这里需要注意的是:
1.默认情况下,系统实际上并不会返回所有的数据,而是默认返回10条数据记录,你可以通过setLimit方法设置返回的记录数量。更多细节可点击查看分页查询一节。
2.当查询的是用户表这种系统表的时候,返回的是BmobUser的数组,设备表,角色表也是这样的。
3.查询用户表,设备表、角色表为:
BmobQuery *bquery = [BmobUser query]; //用户表
BmobQuery *bquery = [BmobInstallation query]; //设备表
BmobQuery *bquery = [BmobRole query]; //角色表
*/
//往GameScore表添加一条playerName为小明,分数为78的数据
// BmobObject *gameScore = [BmobObject objectWithClassName:@"GameScore"];
// [gameScore setObject:@"小明" forKey:@"playerName"];
// [gameScore setObject:@78 forKey:@"score"];
// [gameScore setObject:[NSNumber numberWithBool:YES] forKey:@"cheatMode"];
// [gameScore saveInBackgroundWithResultBlock:^(BOOL isSuccessful, NSError *error) {
// //进行操作
// }];
// NSLog(@"%@",gameScore);
// -----------查找数据
// GameScore 表的名称
// playerName 第二列的名字
// objectId第一列
// createdAt第三列
// updatedAt第四列
BmobQuery *bquery = [BmobQuery queryWithClassName:@"myImage"];
//查找GameScore表的数据
[bquery findObjectsInBackgroundWithBlock:^(NSArray *array, NSError *error) {
for (BmobObject *obj in array) {
//打印playerName
NSLog(@"obj.playerName = %@", [obj objectForKey:@"image1"]);
//打印objectId,createdAt,updatedAt
// NSLog(@"obj.objectId = %@", [obj objectId]);
// NSLog(@"obj.createdAt = %@", [obj createdAt]);
// NSLog(@"obj.updatedAt = %@", [obj updatedAt]);
[dic setObject:[obj objectForKey:@"image1"] forKey:@"image1"];
[dic setObject:[obj objectForKey:@"image2"] forKey:@"image2"];
NSLog(@"---0099%@",dic);
//网络请求
NSURL *url = [NSURL URLWithString:dic[@"image1"]];
NSData *data = [NSData dataWithContentsOfURL:url];
NSURL *url1 = [NSURL URLWithString:dic[@"image2"]];
NSData *data1 = [NSData dataWithContentsOfURL:url1];
NSArray *array = @[data,data1];
for (int i = 0; i < 2; i ++) {
UIImageView *image = [[UIImageView alloc]initWithFrame:CGRectMake(10, 100+(20+100)*i, 100,100)];
image.image = [UIImage imageWithData:array[i]];
[self.view addSubview:image];
}
}
}];
// NSURL *url1 = [NSURL URLWithString:@"http://pic26.nipic.com/20121223/9252150_195341264315_2.jpg"];
// NSData *data1 = [NSData dataWithContentsOfURL:url1];
//
// UIImageView *image = [[UIImageView alloc]initWithFrame:CGRectMake(10, 100, 100,100)];
// image.backgroundColor = [UIColor redColor];
// image.image = [UIImage imageWithData:data1];
// [self.view addSubview:image];
}