0

在我的应用程序中,只有 2 个视图。第一个视图是一个集合视图,当您点击图像时,它会转到MWPhotoBrowser以查看完整大小的图像。

我正在通过互联网检索约 100 张图像并将它们添加到NSMutableArray. 这有问题。当我第一次运行该应用程序并从网格中选择一个缩略图时,它会显示最后一个图像。不是我选的那个。请注意,我第一次强调,因为它只发生在第一次。如果我返回并点击另一张图片,它会正确显示所选图片。下面是它应该如何工作的屏幕截图。

在此处输入图像描述

这是代码

#import "UIImageView+WebCache.h"
#import "ViewController.h"
#import "MWPhotoBrowser.h"
#import "Image.h"
#import "ImageCell.h"

@interface ViewController () <UICollectionViewDelegate, UICollectionViewDataSource, MWPhotoBrowserDelegate>

@property (strong, nonatomic) NSMutableArray *images;
@property (strong, nonatomic) NSMutableArray *browserImages;
@property (strong, nonatomic) MWPhotoBrowser *browser;

@end

@implementation ViewController

- (void)viewDidLoad
{
    [super viewDidLoad];

    self.browser = [[MWPhotoBrowser alloc] initWithDelegate:self];
    self.browser.displayActionButton = YES;
    self.browser.displayNavArrows = YES;
    self.browser.displaySelectionButtons = NO;
    self.browser.zoomPhotosToFill = YES;
    self.browser.alwaysShowControls = YES;
    self.browser.enableGrid = NO;
    self.browser.startOnGrid = NO;
    self.browser.wantsFullScreenLayout = NO;

    [self.browser showNextPhotoAnimated:YES];
    [self.browser showPreviousPhotoAnimated:YES];

    [self loadImages];
}

- (void)loadImages
{
    self.images = [[NSMutableArray alloc] init];
    self.browserImages = [[NSMutableArray alloc] init];

    NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:@"%@%@", HOST_URL, @"All_Images.php"]];
    NSData *jsonResults = [NSData dataWithContentsOfURL:url];
    NSDictionary *results = [NSJSONSerialization JSONObjectWithData:jsonResults options:0 error:NULL];

    NSDictionary *images = results[@"Images"];
    for (NSDictionary *img in images) {
        Image *imageObj = [[Image alloc] init];
        imageObj.imageId = [img objectForKey:@"id"];
        imageObj.imageName = [img objectForKey:@"name"];

        // Get the full image path
        NSString *fullImagePath = [NSString stringWithFormat:@"%@%@", HOST_URL, [img objectForKey:@"full_image"]];
        imageObj.imagePath = fullImagePath;

        // Get the thumbnail image path depending on the device
        NSString *thumbnailPath;
        if (DEVICE_IS_PAD) {
            thumbnailPath = [NSString stringWithFormat:@"%@%@", HOST_URL, [img objectForKey:@"thumbnail_ipad"]];
        } else {
            thumbnailPath = [NSString stringWithFormat:@"%@%@", HOST_URL, [img objectForKey:@"thumbnail_iphone"]];
        }
        imageObj.imageThumbnail = thumbnailPath;

        // Creates an object for each image and fill it with the retrieved info
        [self.images addObject:imageObj];

        // This array stores the image paths for later use (displaying them in a photo browser)
        MWPhoto *browserImage = [MWPhoto photoWithURL:[NSURL URLWithString:imageObj.imagePath]];
        browserImage.caption = imageObj.imageName;
        [self.browserImages addObject:browserImage];
    }
    [self.collectionView reloadData];
}

#pragma mark - MWPhotoBrowserDelegate
- (NSUInteger)numberOfPhotosInPhotoBrowser:(MWPhotoBrowser *)photoBrowser
{
    return self.browserImages.count;
}

- (id <MWPhoto>)photoBrowser:(MWPhotoBrowser *)photoBrowser photoAtIndex:(NSUInteger)index
{
    if (index < self.browserImages.count) {
        return self.browserImages[index];
    }
    return nil;
}

#pragma mark - UICollectionViewDataSource
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return self.images.count;
}

- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
    return 1;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"ImageCell";
    ImageCell *cell = (ImageCell *)[collectionView dequeueReusableCellWithReuseIdentifier:CellIdentifier forIndexPath:indexPath];

    Image *image = self.images[indexPath.row];
    [cell.imageView setImageWithURL:[NSURL URLWithString:image.imageThumbnail] placeholderImage:[UIImage imageNamed:@"placeholder"]];

    return cell;
}

#pragma mark - UICollectionViewDelegate
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    // Opens the image browser
    [self.browser setCurrentPhotoIndex:indexPath.row];
    [self.navigationController pushViewController:self.browser animated:YES];
}

@end

我不知道为什么它在我第一次选择图像时显示网格的最后一张图像。我已经上传了一个包含该问题的演示项目,因此您可以更轻松地快速查看它。 https://www.dropbox.com/s/tp6a67f83l0rzin/PhotoBrowserTest.zip

我真的很感谢任何人帮助纠正这个问题。

谢谢你。

4

1 回答 1

2

在您的项目中更改这些方法

- (NSUInteger)numberOfPhotos {
   if (_photoCount == NSNotFound || _photoCount == 0) {
       if ([_delegate respondsToSelector:@selector(numberOfPhotosInPhotoBrowser:)]) {
           _photoCount = [_delegate numberOfPhotosInPhotoBrowser:self];
       } else if (_depreciatedPhotoData) {
        _photoCount = _depreciatedPhotoData.count;
       }
   }
   if (_photoCount == NSNotFound) _photoCount = 0;
   return _photoCount;
}

- (id)initWithDelegate:(id <MWPhotoBrowserDelegate>)delegate {
    if ((self = [self init])) {
        _delegate = delegate;
        [self setCurrentPhotoIndex:0];
    }
    return self;
}

您需要检查您的 _photoCount == 0,因为第一次您没有此信息并且 _photoCount 将为 0,但第二次您的 numberOfPhotosInPhotoBrowser 将是正确的

于 2014-03-05T08:39:05.967 回答