简体   繁体   English

Parse.com保存图像样本。 只检索一张图像

[英]Parse.com Saving images sample. retrieve only one image

I have set up the sample from parse.com where you can upload an image from the camera and then display all the images in a grid view. 我已经从parse.com设置了示例,您可以在其中从相机上传图像,然后以网格视图显示所有图像。

What I want is to display only the recent image taken instead of a total grid view. 我想要的是仅显示最近拍摄的图像,而不是总的网格视图。

The sample is located here : Parse example 该示例位于此处: 解析示例

The images is being downloaded and placed in some sort of grid view. 正在下载图像并将其放置在某种网格视图中。

The use NSMutableArray *allImages; 使用NSMutableArray *allImages; to get all the images. 获取所有图像。

The images is being output with button classes and a link to a new view. 正在使用按钮类和指向新视图的链接输出图像。

The new view then only contains the image from the grid view (the one you click). 然后,新视图仅包含来自网格视图的图像(您单击的图像)。

I would like to instead of downloading all of the images and placing them inside a grid view - just to display the latest image. 我不想下载所有图像并将它们放置在网格视图中,而只是为了显示最新图像。

They code used to take the image from a small thumbnail to the full image view is like this: 他们使用以下代码将图像从较小的缩略图拍摄到完整的图像视图,如下所示:

// 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];

The code to get allImages is like: 获取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;
        });
    });

I'm not an expert at this iOS-coding and I'm really just looking for a simple solution to display the recent image posted. 我不是该iOS编码的专家,我只是在寻找一种简单的解决方案来显示发布的最新图片。

Looking through the posted example code, it would be a big undertaking to modify it to handle just a one image (mostly deleting a lot of yuck that synchs object ids). 查看发布的示例代码,将其修改为仅处理一个图像将是一项艰巨的任务(主要是删除同步对象ID的许多麻烦)。 If you just want the quickest path to get started, you can do this: 如果您只想以最快的方式上手,可以执行以下操作:

// ...
// 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

A lot of UI code can get simpler, too. 许多UI代码也可以变得更简单。 But the key is to skip all of the grid/button building code and do something like this: 但是关键是跳过所有的网格/按钮构建代码并执行以下操作:

 // ...
 // 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;

Hope that helps. 希望能有所帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM