首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >ios如何使用mwphotobrowser加载本地照片。

ios如何使用mwphotobrowser加载本地照片。
EN

Stack Overflow用户
提问于 2014-03-06 23:50:43
回答 2查看 1.4K关注 0票数 0

我已经下载了一个使用mwphotobrowser的示例代码,它真的是cool.but,当我尝试使用它的时候我有问题,我想它可以加载本地照片而不是网址。我一次又一次地尝试,但failed...here是下面的代码:

代码语言:javascript
复制
#import "UIImageView+WebCache.h"
#import "MWPhotoBrowser.h"
#import "Image.h"
#import "ImageCell.h"

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

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

@end

@implementation DEMOSevenViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
    // Custom initialization
    }
    return self;
}    

- (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
{
NSLog(@"Count: %lu", (unsigned long)self.browserImages.count);
return self.browserImages.count;
//    return 93;
}

- (id <MWPhoto>)photoBrowser:(MWPhotoBrowser *)photoBrowser photoAtIndex:(NSUInteger)index
{
NSLog(@"Index: %lu", (unsigned long)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
//    NSLog(@"Selected Index: %ld", (long)indexPath.row);
[self.browser setCurrentPhotoIndex:indexPath.row];
[self.navigationController pushViewController:self.browser animated:YES];
}

- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}


@end

我真的需要这方面的帮助,请帮助你非常me.thank。it's the sample code from the cool man

EN

回答 2

Stack Overflow用户

发布于 2014-04-04 11:25:26

使用delegate函数:

代码语言:javascript
复制
-(NSUInteger)numberOfPhotosInPhotoBrowser:(MWPhotoBrowser *)photoBrowser {
return [photoShowArray count];

}

代码语言:javascript
复制
-(NSUInteger)numberOfPhotosInPhotoBrowser:(MWPhotoBrowser *)photoBrowser {
return [photoShowArray count];

}

代码语言:javascript
复制
-(id <MWPhoto>)photoBrowser:(MWPhotoBrowser *)photoBrowser photoAtIndex:(NSUInteger)index {
return [photoShowArray objectAtIndex:index];

}

代码语言:javascript
复制
-(id <MWPhoto>)photoBrowser:(MWPhotoBrowser *)photoBrowser thumbPhotoAtIndex:(NSUInteger)index {
return [thumbnailsArray objectAtIndex:index];

}

并使用MWPhoto photoWithUrl:加载您的图像,因为它有缓存。您可以使用NSURL fileURLWithPath:path将本地路径转换为url

票数 1
EN

Stack Overflow用户

发布于 2014-03-06 23:59:44

打印您的图像路径并尝试在浏览器中打开它

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/22229371

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档