无法在 iOS 的 MWPhotoBrowser 中添加多张照片 url
Unable to add multiple photo url in MWPhotoBrowser in iOS
无法在 for 循环中添加多个 url。怎么可能。如果我可以在没有循环的情况下使用单个 url 那么它的工作。但不能与多个 url 循环工作。
我该如何解决这个问题
我试过下面的代码:
- (NSUInteger)numberOfPhotosInPhotoBrowser:(MWPhotoBrowser *)photoBrowser {
return _photos.count;
}
- (MWPhoto *)photoBrowser:(MWPhotoBrowser *)photoBrowser photoAtIndex:(NSUInteger)index {
if (index < _photos.count)
return [_photos objectAtIndex:index];
return nil;
}
- (void)viewDidLoad
{
[super viewDidLoad];
NSMutableArray *photos = [[NSMutableArray alloc] init];
photos=[[NSMutableArray alloc]initWithObjects:@"http://gloucesterstage.com/_website/wp-content/uploads/2015/06/apples2.jpg",@"http://www.apple.com/ipad/home/images/social/og.jpg?201508031746",nil];
MWPhoto *photo;
for (int i=0;i<[photos count];i++) {
photo = [MWPhoto photoWithURL:[NSURL URLWithString:photos[i]]];
photo.caption = @"";
[photos addObject:photo];
}
_photos = photos;
MWPhotoBrowser *browser = [[MWPhotoBrowser alloc] initWithDelegate:self];
UINavigationController *nc = [[UINavigationController alloc] initWithRootViewController:browser];
[browser showNextPhotoAnimated:YES];
[browser showPreviousPhotoAnimated:YES];
[self presentViewController:nc animated:YES completion:nil];
}
您似乎想显示本地照片。为此,您需要使用下面的内容。
[photos addObject:[MWPhoto photoWithURL:[NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:@"photo2l" ofType:@"jpg"]]]];
Reference
编辑 1
根据您的新更新,您使用的是在线图片。使用下面的代码。
NSMutableArray *photos = [[NSMutableArray alloc] init];
MWPhoto *photo;
NSMutableArray *anotherArrayWhereYouHaveImages=[[NSMutableArray alloc] initWithObjects:@"http://gloucesterstage.com/_website/wp-content/uploads/2015/06/apples2.jpg",@"http://www.apple.com/ipad/home/images/social/og.jpg?201508031746",nil];
for (int i=0;i<[anotherArrayWhereYouHaveImages count];i++) {
photo = [MWPhoto photoWithURL:[NSURL URLWithString:[anotherArrayWhereYouHaveImages objectAtIndex:i]]];
photo.caption = [NSString stringWithFormat:@"Photo %d", i];
[photos addObject:photo];
}
self.photos = photos;
// Create browser
MWPhotoBrowser *browser = [[MWPhotoBrowser alloc] initWithDelegate:self];
browser.displayActionButton = YES;
browser.displayNavArrows = YES;
browser.zoomPhotosToFill = NO;
browser.navigationController.navigationBarHidden = NO;
[browser setCurrentPhotoIndex:myPostForPhoto];
CATransition *transition = [CATransition animation];
transition.duration = 0.5;
transition.type = kCATransitionFade;
[self.navigationController.view.layer addAnimation:transition forKey:kCATransition];
[self.navigationController pushViewController:browser animated:NO];
无法在 for 循环中添加多个 url。怎么可能。如果我可以在没有循环的情况下使用单个 url 那么它的工作。但不能与多个 url 循环工作。 我该如何解决这个问题 我试过下面的代码:
- (NSUInteger)numberOfPhotosInPhotoBrowser:(MWPhotoBrowser *)photoBrowser {
return _photos.count;
}
- (MWPhoto *)photoBrowser:(MWPhotoBrowser *)photoBrowser photoAtIndex:(NSUInteger)index {
if (index < _photos.count)
return [_photos objectAtIndex:index];
return nil;
}
- (void)viewDidLoad
{
[super viewDidLoad];
NSMutableArray *photos = [[NSMutableArray alloc] init];
photos=[[NSMutableArray alloc]initWithObjects:@"http://gloucesterstage.com/_website/wp-content/uploads/2015/06/apples2.jpg",@"http://www.apple.com/ipad/home/images/social/og.jpg?201508031746",nil];
MWPhoto *photo;
for (int i=0;i<[photos count];i++) {
photo = [MWPhoto photoWithURL:[NSURL URLWithString:photos[i]]];
photo.caption = @"";
[photos addObject:photo];
}
_photos = photos;
MWPhotoBrowser *browser = [[MWPhotoBrowser alloc] initWithDelegate:self];
UINavigationController *nc = [[UINavigationController alloc] initWithRootViewController:browser];
[browser showNextPhotoAnimated:YES];
[browser showPreviousPhotoAnimated:YES];
[self presentViewController:nc animated:YES completion:nil];
}
您似乎想显示本地照片。为此,您需要使用下面的内容。
[photos addObject:[MWPhoto photoWithURL:[NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:@"photo2l" ofType:@"jpg"]]]];
Reference
编辑 1
根据您的新更新,您使用的是在线图片。使用下面的代码。
NSMutableArray *photos = [[NSMutableArray alloc] init];
MWPhoto *photo;
NSMutableArray *anotherArrayWhereYouHaveImages=[[NSMutableArray alloc] initWithObjects:@"http://gloucesterstage.com/_website/wp-content/uploads/2015/06/apples2.jpg",@"http://www.apple.com/ipad/home/images/social/og.jpg?201508031746",nil];
for (int i=0;i<[anotherArrayWhereYouHaveImages count];i++) {
photo = [MWPhoto photoWithURL:[NSURL URLWithString:[anotherArrayWhereYouHaveImages objectAtIndex:i]]];
photo.caption = [NSString stringWithFormat:@"Photo %d", i];
[photos addObject:photo];
}
self.photos = photos;
// Create browser
MWPhotoBrowser *browser = [[MWPhotoBrowser alloc] initWithDelegate:self];
browser.displayActionButton = YES;
browser.displayNavArrows = YES;
browser.zoomPhotosToFill = NO;
browser.navigationController.navigationBarHidden = NO;
[browser setCurrentPhotoIndex:myPostForPhoto];
CATransition *transition = [CATransition animation];
transition.duration = 0.5;
transition.type = kCATransitionFade;
[self.navigationController.view.layer addAnimation:transition forKey:kCATransition];
[self.navigationController pushViewController:browser animated:NO];