我正在做一个IOS项目,在这个项目中,我需要能够进入包含ViewController的MWPhotoBrowser。
目前,我已经创建了一个PhotoGalleryController,并将其用作MWPhotoBrowser的容器,因为MWPhotoBrowser是UIViewController的一个子类。
但是,当我在模拟器中启动应用程序时,我会得到一个中间有一个圆圈的空白屏幕,这意味着PhotoBrowser已经加载,但是没有返回照片。
我继续记录我的所有委托方法,发现numberOfPhotos正在被调用,但是应该返回MWPhoto对象的实际函数从未被调用过!
我不太清楚为什么照片对象的实际加载从未完成,即使我已经在容器ViewController中实现了所有正确的委托方法。
我检查了API调用给出的响应的输出,并打印出了self.photoList。这些看起来很好,所以我不知道它为什么不归还任何照片。
PhotoGalleryController.m
#import "PhotoGalleryController.h"
#import "MZApi.h"
#import "MZUser.h"
@implementation PhotoGalleryController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
//Make the HTTP request and setup the photo array in MZUser. Change from "usertext" later.
[MZApi loadOwnPhotoWithUserID:@"usertext" andCompletionHandler:^(NSMutableArray *photos, NSError *error) {
if(photos!=nil){
self.userPhotoList = photos;
[[MZUser getCurrentUser]setPhotoList:self.userPhotoList];
//NSLog(@" %@", self.userPhotoList);
//Should currently be null because CurrentUser is not created yet.
}
else{
NSLog(@"Failed to allocate photos!");
}
}];
MWPhotoBrowser *browser = [[MWPhotoBrowser alloc]initWithDelegate:self];
browser.displayActionButton = YES; // Show action button to allow sharing, copying, etc (defaults to YES)
browser.displayNavArrows = NO; // Whether to display left and right nav arrows on toolbar (defaults to NO)
browser.displaySelectionButtons = NO; // Whether selection buttons are shown on each image (defaults to NO)
browser.enableGrid = YES; // Whether to allow the viewing of all the photo thumbnails on a grid (defaults to YES)
browser.startOnGrid = YES; // Whether to start on the grid of thumbnails instead of the first photo (defaults to NO)
[self.view addSubview:browser.view];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
-(NSUInteger) numberOfPhotosInPhotoBrowser:(MWPhotoBrowser *)photoBrowser{
return [self.userPhotoList count];
}
- (id <MWPhoto>)photoBrowser:(MWPhotoBrowser *)photoBrowser photoAtIndex:(NSUInteger)index{
if(index<self.userPhotoList.count){
NSString *temp = [[self.userPhotoList objectAtIndex:index]file_url];
MWPhoto *photo = [MWPhoto photoWithURL:[NSURL URLWithString:temp]];
return photo;
}
return nil;
}
-(id <MWPhoto>) photoBrowser:(MWPhotoBrowser *)photoBrowser thumbPhotoAtIndex:(NSUInteger)index{
NSLog(@"Thumb Delegate Called!");
if(index<self.userPhotoList.count){
NSString *temp = [[self.userPhotoList objectAtIndex:index]file_url];
MWPhoto *photo = [MWPhoto photoWithURL:[NSURL URLWithString:temp]];
return photo;
}
return nil;
}
@endPhotoGalleryController.h
#import <UIKit/UIKit.h>
#import "MWPhotoBrowser.h"
@interface PhotoGalleryController : UIViewController <MWPhotoBrowserDelegate>
@property(strong, nonatomic) NSMutableArray * userPhotoList;
- (NSUInteger)numberOfPhotosInPhotoBrowser:(MWPhotoBrowser *)photoBrowser;
- (id <MWPhoto>)photoBrowser:(MWPhotoBrowser *)photoBrowser photoAtIndex:(NSUInteger)index;
- (id <MWPhoto>)photoBrowser:(MWPhotoBrowser *)photoBrowser thumbPhotoAtIndex:(NSUInteger)index;
@end服务器响应示例:
(
{
dislikes = 0;
"file_url" = "http://localhost:3000/static/photos/2.jpg";
likes = 0;
"photo_id" = 2;
"user_id" = kai1234;
},
{
dislikes = 4;
"file_url" = "http://localhost:3000/static/photos/1.jpg";
likes = 5;
"photo_id" = 1;
"user_id" = kai1234;
}
)发布于 2016-04-17 22:19:11
在加载带有块的照片时,需要添加浏览器来查看,通常在用块加载照片时会出现延迟,因此浏览器将获得一个空对象。
您的代码应该看起来像
#import "PhotoGalleryController.h"
#import "MZApi.h"
#import "MZUser.h"
@implementation PhotoGalleryController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
//Make the HTTP request and setup the photo array in MZUser. Change from "usertext" later.
self.userPhotoList = [NSMutableArray array];
[MZApi loadOwnPhotoWithUserID:@"usertext" andCompletionHandler:^(NSMutableArray *photos, NSError *error) {
if(photos!=nil){
self.userPhotoList = photos;
[[MZUser getCurrentUser] setPhotoList:self.userPhotoList];
//NSLog(@" %@", self.userPhotoList);
//Should currently be null because CurrentUser is not created yet.
for (int i = 0; i < photos.count; i++)
{
[self.userPhotoList addObject:[MWPhoto photoWithURL:[NSURL URLWithString:[[photos objectAtIndex:i] file_url]]];
}
MWPhotoBrowser *browser = [[MWPhotoBrowser alloc]initWithDelegate:self];
browser.displayActionButton = YES; // Show action button to allow sharing, copying, etc (defaults to YES)
browser.displayNavArrows = NO; // Whether to display left and right nav arrows on toolbar (defaults to NO)
browser.displaySelectionButtons = NO; // Whether selection buttons are shown on each image (defaults to NO)
browser.enableGrid = YES; // Whether to allow the viewing of all the photo thumbnails on a grid (defaults to YES)
browser.startOnGrid = YES; // Whether to start on the grid of thumbnails instead of the first photo (defaults to NO)
[self.view addSubview:browser.view];
}
else{
NSLog(@"Failed to allocate photos!");
}
}];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
-(NSUInteger) numberOfPhotosInPhotoBrowser:(MWPhotoBrowser *)photoBrowser{
return [self.userPhotoList count];
}
- (id <MWPhoto>)photoBrowser:(MWPhotoBrowser *)photoBrowser photoAtIndex:(NSUInteger)index{
if(index<self.userPhotoList.count){
return [self.userPhotoList objectAtIndex:index];
}
return nil;
}
-(id <MWPhoto>) photoBrowser:(MWPhotoBrowser *)photoBrowser thumbPhotoAtIndex:(NSUInteger)index{
NSLog(@"Thumb Delegate Called!");
if(index<self.userPhotoList.count){
return [self.userPhotoList objectAtIndex:index];
}
return nil;
}
@endhttps://stackoverflow.com/questions/36682406
复制相似问题