代码之家  ›  专栏  ›  技术社区  ›  Isuru PapaSmurf

从集合视图中选择时,MWPhotoBrowser未显示正确的图像

  •  0
  • Isuru PapaSmurf  · 技术社区  · 12 年前

    在我的应用程序中,只有2个视图。第一个视图是集合视图,当您点击图像时,它将转到 MWPhotoBrowser 以查看全尺寸图像。

    我正在互联网上检索约100张图片,并将它们添加到 NSMutableArray . 这有问题。当我为 第一次 并从显示最后图像的网格中选择缩略图图像。不是我选的那个。注意,我第一次强调,因为它只发生在第一次。如果我返回并点击另一个图像,它将正确显示所选图像。以下是其如何 想象上的 工作。

    enter image description here

    这是密码

    #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

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

    非常感谢。

    1 回复  |  直到 12 年前
        1
  •  2
  •   Fran Martin    12 年前

    在项目中更改这些方法

    - (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,因为第一次您没有此信息时,_photoCunt将为0,但第二次您的numberOfPhotoInPhotoBrowser将正确

    推荐文章