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

ios如何使用mwphotobrowser加载本地照片。,ios,view,collections,local,photo,Ios,View,Collections,Local,Photo,我已经下载了一个使用mwphotobrowser的示例代码,它真的很酷。但是我在尝试使用它时遇到了问题,我希望它可以加载本地照片而不是URL。我尝试了一次又一次,但失败了…下面是代码: #import "UIImageView+WebCache.h" #import "MWPhotoBrowser.h" #import "Image.h" #import "ImageCell.h" @interface DEMOSevenViewController () <UICollectionVi

我已经下载了一个使用mwphotobrowser的示例代码,它真的很酷。但是我在尝试使用它时遇到了问题,我希望它可以加载本地照片而不是URL。我尝试了一次又一次,但失败了…下面是代码:

#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
#导入“UIImageView+WebCache.h”
#导入“MWPhotoBrowser.h”
#导入“Image.h”
#导入“ImageCell.h”
@接口DemoSeveniewController()
@属性(强,非原子)NSMutableArray*图像;
@属性(强,非原子)NSMutableArray*BrowserImage;
@属性(强,非原子)MWPhotoBrowser*浏览器;
@结束
@DemoSeveniewController的实现
-(id)initWithNibName:(NSString*)nibNameOrNil bundle:(NSBundle*)nibBundleOrNil
{
self=[super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
如果(自我){
//自定义初始化
}
回归自我;
}    
-(无效)viewDidLoad
{
[超级视图下载];
self.browser=[[MWPhotoBrowser alloc]initWithDelegate:self];
self.browser.displayActionButton=是;
self.browser.displayNavArrows=是;
self.browser.displaySelectionButtons=否;
self.browser.zoomPhotosToFill=是;
self.browser.alwaysShowControls=是;
self.browser.enableGrid=否;
self.browser.startOnGrid=否;
self.browser.wantsFullScreenLayout=否;
[self.browser showNextPhotoAnimated:是];
[self.browser showPreviousPhotoAnimated:是];
[自动加载图像];
}
-(void)加载图像
{
self.images=[[NSMutableArray alloc]init];
self.browserImages=[[NSMutableArray alloc]init];
NSURL*url=[NSURL URLWithString:[NSString stringWithFormat:@“%@%@”、主机url、@“All_Images.php”];
NSData*jsonResults=[NSData datawithcontentsofull:url];
NSDictionary*results=[NSJSONSerialization JSONObjectWithData:jsonResults选项:0错误:NULL];
NSDictionary*图像=结果[@“图像”];
用于(NSDictionary*图像中的img){
Image*imageObj=[[Image alloc]init];
imageObj.imageId=[img objectForKey:@“id”];
imageObj.imageName=[img objectForKey:@“name”];
//获取完整的图像路径
NSString*fullImagePath=[NSString stringWithFormat:@“%@%@”,主机URL,[img objectForKey:@“完整图像”];
imageObj.imagePath=完整的imagePath;
//根据设备获取缩略图图像路径
NSString*缩略图路径;
如果(设备为焊盘){
thumbnailPath=[NSString stringWithFormat:@“%@%@”,主机URL[img objectForKey:@“thumbnail\U ipad”];
}否则{
thumbnailPath=[NSString stringWithFormat:@“%@%@”,主机URL,[img objectForKey:@“thumbnail\U iphone”];
}
imageObj.imageThumbnail=缩略图路径;
//为每个图像创建一个对象,并用检索到的信息填充它
[self.images addObject:imageObj];
//此阵列存储图像路径供以后使用(在照片浏览器中显示)
MWPhoto*browserImage=[MWPhoto photoWithURL:[NSURL URLWithString:imageObj.imagePath]];
browserImage.caption=imageObj.imageName;
[self.browserImages addObject:browserImage];
}
[self.collectionView-reloadData];
}
#pragma标记-MWPhotoBrowserDelegate
-(NSInteger)numberOfPhotosInPhotoBrowser:(MWPhotoBrowser*)photoBrowser
{
NSLog(@“计数:%lu”,(无符号长)self.browserImages.Count);
返回self.browserImages.count;
//返回93;
}
-(id)photoBrowser:(MWPhotoBrowser*)photoBrowser photoAtIndex:(NSUInteger)索引
{
NSLog(@“索引:%lu”,(无符号长)索引);
如果(索引

我真的需要帮助,请帮助我。非常感谢。

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

使用代理功能:

-(NSUInteger)numberOfPhotosInPhotoBrowser:(MWPhotoBrowser *)photoBrowser {
return [photoShowArray count];
-(NSUInteger)numberOfPhotosInPhotoBrowser:(MWPhotoBrowser *)photoBrowser {
return [photoShowArray count];
}

}

-(id)photoBrowser:(MWPhotoBrowser*)photoBrowser photoAtIndex:(nsInteger)索引{
返回[photoShowArray objectAtIndex:index];
}

-(id)photoBrowser:(MWPhotoBrowser*)photoBrowser thumbPhotoAtIndex:(nsInteger)索引{
返回[thumbnailsArray对象索引:索引];
}

并使用[MWPhoto photoWithUrl:]加载具有缓存的图像。
您可以使用[NSURL fileURLWithPath:path]

将本地路径转换为url,只需使用
NSURL fileURLWithPath:
创建带有本地文件路径的
NSURL
。非常感谢,请告诉我如何创建。我是一名新手。非常感谢,但我无法理解本地文件路径,这对我来说很困难。
-(id <MWPhoto>)photoBrowser:(MWPhotoBrowser *)photoBrowser photoAtIndex:(NSUInteger)index {
return [photoShowArray objectAtIndex:index];
-(id <MWPhoto>)photoBrowser:(MWPhotoBrowser *)photoBrowser thumbPhotoAtIndex:(NSUInteger)index {
return [thumbnailsArray objectAtIndex:index];