Parse.com 保存图像示例。只检索一张图像
Posted
技术标签:
【中文标题】Parse.com 保存图像示例。只检索一张图像【英文标题】:Parse.com Saving images sample. retrieve only one image 【发布时间】:2014-09-25 13:07:52 【问题描述】:我从 parse.com 设置了示例,您可以在其中从相机上传图像,然后在网格视图中显示所有图像。
我想要的是只显示最近拍摄的图像而不是总网格视图。
示例位于此处:Parse example
正在下载图像并将其放置在某种网格视图中。
使用NSMutableArray *allImages;
获取所有图片。
输出的图像带有按钮类和指向新视图的链接。
然后新视图只包含来自网格视图(您单击的那个)的图像。
我不想下载所有图像并将它们放在网格视图中 - 只是为了显示最新图像。
他们用于将图像从小缩略图获取到完整图像视图的代码是这样的:
// When picture is touched, open a viewcontroller with the image
PFObject *theObject = (PFObject *)[allImages objectAtIndex:[sender tag]];
PFFile *theImage = [theObject objectForKey:@"imageFile"];
NSData *imageData;
imageData = [theImage getData];
UIImage *selectedPhoto = [UIImage imageWithData:imageData];
PhotoDetailViewController *pdvc = [[PhotoDetailViewController alloc] init];
pdvc.selectedImage = selectedPhoto;
[self presentViewController:pdvc animated:YES completion:nil];
获取allImages的代码如下:
// Contains a list of all the BUTTONS
allImages = [images mutableCopy];
// This method sets up the downloaded images and places them nicely in a grid
dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
dispatch_async(queue, ^
NSMutableArray *imageDataArray = [NSMutableArray array];
// Iterate over all images and get the data from the PFFile
for (int i = 0; i < images.count; i++)
PFObject *eachObject = [images objectAtIndex:i];
PFFile *theImage = [eachObject objectForKey:@"imageFile"];
NSData *imageData = [theImage getData];
UIImage *image = [UIImage imageWithData:imageData];
[imageDataArray addObject:image];
// Dispatch to main thread to update the UI
dispatch_async(dispatch_get_main_queue(), ^
// Remove old grid
for (UIView *view in [photoScrollView subviews])
if ([view isKindOfClass:[UIButton class]])
[view removeFromSuperview];
// Create the buttons necessary for each image in the grid
for (int i = 0; i < [imageDataArray count]; i++)
PFObject *eachObject = [images objectAtIndex:i];
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
UIImage *image = [imageDataArray objectAtIndex:i];
[button setImage:image forState:UIControlStateNormal];
button.showsTouchWhenHighlighted = YES;
[button addTarget:self action:@selector(buttonTouched:) forControlEvents:UIControlEventTouchUpInside];
button.tag = i;
button.frame = CGRectMake(THUMBNAIL_WIDTH * (i % THUMBNAIL_COLS) + PADDING * (i % THUMBNAIL_COLS) + PADDING,
THUMBNAIL_HEIGHT * (i / THUMBNAIL_COLS) + PADDING * (i / THUMBNAIL_COLS) + PADDING + PADDING_TOP,
THUMBNAIL_WIDTH,
THUMBNAIL_HEIGHT);
button.imageView.contentMode = UIViewContentModeScaleAspectFill;
[button setTitle:[eachObject objectId] forState:UIControlStateReserved];
[photoScrollView addSubview:button];
// Size the grid accordingly
int rows = images.count / THUMBNAIL_COLS;
if (((float)images.count / THUMBNAIL_COLS) - rows != 0)
rows++;
int height = THUMBNAIL_HEIGHT * rows + PADDING * rows + PADDING + PADDING_TOP;
photoScrollView.contentSize = CGSizeMake(self.view.frame.size.width, height);
photoScrollView.clipsToBounds = YES;
);
);
我不是 ios 编码方面的专家,我只是在寻找一种简单的解决方案来显示最近发布的图像。
【问题讨论】:
您是要获取所有图像并呈现最近的图像,还是仅获取最近的图像并呈现它? 对我来说这并不重要。无论是最省电的手机还是最简单的方式。我只需要显示在 UIImageView 中拍摄的最近图像。 Ty 回复 【参考方案1】:查看发布的示例代码,修改它以仅处理一个图像(主要是删除很多同步对象 id 的恶心)将是一项艰巨的任务。如果您只想以最快的方式开始使用,您可以这样做:
// ...
// where you do the query for UserPhoto
[query orderByAscending:@"createdAt"];
// use the getFirst form of the query
[query getFirstObjectInBackgroundWithBlock:^(PFObject *object, NSError *error)
if (!error)
// build the objects array that the demo code expects, but out of just one object
NSArray *objects = @[ object ];
// the remainder of the code as it is
很多 UI 代码也可以变得更简单。但关键是跳过所有网格/按钮构建代码并执行以下操作:
// ...
// inside the method called setUpImages:
// this loop will only run once since we have only one object in the array
for (int i = 0; i < [imageDataArray count]; i++)
PFObject *eachObject = [images objectAtIndex:i];
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
UIImage *image = [imageDataArray objectAtIndex:i];
// you're essentially done here. add a big image view
// that replaces photoScrollView, then:
self.myBigImageView.image = image;
希望对您有所帮助。
【讨论】:
泰。我以前不明白你的帖子。我会试试这个并回复你。以上是关于Parse.com 保存图像示例。只检索一张图像的主要内容,如果未能解决你的问题,请参考以下文章