I want to show multiple videos in a collection view. I have this code:
-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
return array.count;
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:#"Cell" forIndexPath:indexPath];
NSURL *videoURL = [NSURL fileURLWithPath:#"Path"];
AVPlayer *player = [AVPlayer playerWithURL:videoURL];
AVPlayerViewController *playerViewController = [AVPlayerViewController new];
playerViewController.player = player;
playerViewController.showsPlaybackControls = NO;
playerViewController.videoGravity = AVLayerVideoGravityResizeAspectFill;
playerViewController.view.frame = CGRectMake(0, 0, 200, 200);
[cell insertSubview:playerViewController.view atIndex:0];
[player play];
return cell;
}
But the code is very slowly and the scrolling is not smooth. Can you help me?
Yes, I have sample code that does exactly what you want, and more. You can download the code and see a video showing what it does from/at:
http://demonicactivity.blogspot.com/2016/08/draft-what-in-hell-this-demoniac-is-up.html
Related
Hello everyone I have a problem with my app ... Within my View Controller hp a CollectionView with a custom cell that should return all of the photos in the Camera Roll section of the app pictures of my iphone.
Now I've done all the steps to show the photos in a ImageView in the custom cell and up to now I have no problem ... My problem is that when I start to scroll through photos, uploading photos is very slow and immediately after the app crashes giving me back this error in the log ..
[GatekeeperXPC]
Connection to assetsd was interrupted or assetsd died 25/02/2017 20:
[Generic] Creating an image
format with an unknown type is an error
Can you tell me if I've taken the right way to show pictures in my collection view? Where did I go wrong? because my app crashes?
Thank you all for any help you can give me
This is the code i utilize
- (void)viewDidLoad {
[super viewDidLoad];
self.nameTextField.delegate = self;
self.emailTextField.delegate = self;
self.passwordTextField.delegate = self;
self.collectionView.delegate = self;
self.collectionView.dataSource = self;
_collectionView.backgroundColor = [UIColor clearColor];
}
-(void)viewDidAppear:(BOOL)animated {
[super viewDidAppear:animated];
[_nameTextField becomeFirstResponder];
[self queryImage];
}
-(void)queryImage {
PHFetchOptions *fetchOptions = [[PHFetchOptions alloc] init];
PHFetchResult *collection = [PHAssetCollection fetchAssetCollectionsWithType:PHAssetCollectionTypeSmartAlbum subtype:PHAssetCollectionSubtypeSmartAlbumUserLibrary options:fetchOptions];
if (collection.firstObject != nil ) {
_photoFound = YES;
_assetCollection = collection.firstObject;
} else {
}
_photoAsset = [PHAsset fetchAssetsInAssetCollection:_assetCollection options:nil];
[_collectionView reloadData];
}
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
return CGSizeMake(80,80);
}
-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
NSUInteger count = 0;
if (_photoAsset != nil) {
count = [_photoAsset count];
}
return count;
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)cv cellForItemAtIndexPath:(NSIndexPath *)indexPath {
static NSString *reuseIdentifier = #"imageCell";
UPCameraRollCollectionViewCell* cell = [cv dequeueReusableCellWithReuseIdentifier:reuseIdentifier forIndexPath:indexPath];
cell.backgroundColor = [UIColor redColor];
PHAsset *asset = [_photoAsset objectAtIndex:indexPath.item];
PHImageManager *imageManager = [PHImageManager defaultManager];
[imageManager requestImageForAsset:asset targetSize:PHImageManagerMaximumSize contentMode:PHImageContentModeAspectFill options:nil resultHandler:^(UIImage * _Nullable result, NSDictionary * _Nullable info) {
[cell setThumbnailImage:result];
}];
return cell;
}
Use PHCachingImageManager.
Apple has an example that shows exactly how to do the sort of thing you're after. Collection views are precisely the intended use case.
In my UICollectionView i am showing images i want to show the images one after another but its coming all at a time though i have used dispatch_after but its not working
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:#"Cell"forIndexPath:indexPath];
UIImageView *showcardCollectionImageView;
showcardCollectionImageView = [[UIImageView alloc]initWithFrame:CGRectMake(1, 1, cell.frame.size.width - 2, cell.frame.size.height - 2)];
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
NSString *cardImageString = [cardsimagearray objectAtIndex:indexPath.row];
showcardCollectionImageView.image = [UIImage imageNamed:[NSString stringWithFormat:#"%#.png",cardImageString]];
showcardCollectionImageView.hidden = NO;
[cell addSubview:showcardCollectionImageView];
});
return cell;
}
You can use https://github.com/rs/SDWebImage, it's fast downloading image from server.
#import "UIImageView+WebCache.h"
[imgOffer sd_setImageWithURL:[NSURL URLWithString:objRecord.imgPath]
placeholderImage:[UIImage imageNamed:#""]
options:0]
I'm trying to do a UICollectionView of articles that doesn't eats RAM like an elephant. So, the UICollectionView consists of full screen cells that contain one image. If the UICollectionViewCell appears, the content gets loaded in. This is the code:
- (void)viewDidLoad {
UICollectionViewFlowLayout *layout=[[UICollectionViewFlowLayout alloc] init];
layout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
layout.itemSize = CGSizeMake(self.view.frame.size.width, self.view.frame.size.height);
layout.minimumInteritemSpacing = 0;
layout.minimumLineSpacing = 0;
_collectionView = [[UICollectionView alloc] initWithFrame:self.view.frame collectionViewLayout:layout];
[_collectionView setDataSource:self];
[_collectionView setDelegate:self];
_collectionView.pagingEnabled = YES;
[_collectionView registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:#"cellIdentifier"];
[_collectionView setBackgroundColor:[UIColor redColor]];
[self.view addSubview:_collectionView];
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:#"cellIdentifier" forIndexPath:indexPath];
//add UIImageView here
//add UITextView with tag 123 here
return cell;
}
- (void)collectionView:(UICollectionView *)collectionView willDisplayCell:(UICollectionViewCell *)cell forItemAtIndexPath:(NSIndexPath *)indexPath {
UICollectionViewCell *cell = [_collectionView cellForItemAtIndexPath:indexPath];
UITextView *textView = [cell viewWithTag:123];
textView.text = #"Testing this out.";
}
However, this doesn't do anything. No errors are thrown, it just doesn't do anything. What am I doing wrong?
Although UICollectionViewCells is displayed, [collectionView visibleCells] returns nil.
- (void)viewDidLoad{
self.collectionView = [[UICollectionView alloc]initWithFrame:CGRectMake(0, 0, 320, 568) collectionViewLayout:flowLayout];
self.collectionView.delegate = self;
self.collectionView.dataSource = self;
[self.collectionView registerClass:[CustomCell class] forCellWithReuseIdentifier:#"Cell"];
[self.view addSubview:self.collectionView];
NSLog(#"%d", [[self.collectionView visibleCells] count]);
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
CustomCell* cell = [collectionView dequeueReusableCellWithReuseIdentifier:#"Cell" forIndexPath:indexPath];
return cell;
}
Do you have any idea?
The issue is caused by reload has not been finished.
The solution is :
1. [collectionView reloadData];
2. **[collectionView layoutIfNeeded];
3. the get your - > [collectionView visibleCells];
Try this in your view controller
- (void) viewDidLayoutSubviews {
NSLog(#"%d", [[self.collectionView visibleCells] count]);
}
Try add this:
override func viewWillLayoutSubviews() {
super.viewWillLayoutSubviews()
collectionView?.collectionViewLayout.invalidateLayout()
}
i have multiple image in uicollectionview like grid view. i want select multiple image at a time but not working this code.please any one give idea about this code.
i have already tried this code but not working.
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath: (NSIndexPath *)indexPath
{
NSMutableArray *indexPaths = [NSMutableArray arrayWithObject:indexPath];
if (self.selectedItemIndexPath)
{
// if we had a previously selected cell
if ([indexPath compare:self.selectedItemIndexPath] == NSOrderedSame)
{
// if it's the same as the one we just tapped on, then we're unselecting it
self.selectedItemIndexPath = nil;
}
else
{
// if it's different, then add that old one to our list of cells to reload, and
// save the currently selected indexPath
[indexPaths addObject:self.selectedItemIndexPath];
self.selectedItemIndexPath = indexPath;
}
}
else
{
// else, we didn't have previously selected cell, so we only need to save this indexPath for future reference
self.selectedItemIndexPath = indexPath;
}
// and now only reload only the cells that need updating
[self.collectionView reloadItemsAtIndexPaths:indexPaths];
}
// I Have Drag the UICollectionView Controller in storyboard
static NSString * const reuseIdentifier = #"Cell";
- (void)viewDidLoad
{
[super viewDidLoad];
[self.collectionView registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:reuseIdentifier];
arrImage = [[NSMutableArray alloc]initWithObjects:#"1.jpeg",#"2.jpeg",#"3.jpeg",#"4.jpeg",#"5.jpeg",#"6.jpeg",#"7.jpeg",#"8.jpeg",#"9.jpeg",#"10.jpeg",#"flower.jpeg",#"flower1.jpeg", nil];
[self.collectionView setAllowsMultipleSelection:YES];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
#pragma mark <UICollectionViewDataSource>
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
return 1;
}
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
return [arrImage count];
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:reuseIdentifier forIndexPath:indexPath];
UIImageView *recipeImageView = (UIImageView *)[cell viewWithTag:100];
recipeImageView.image = [UIImage imageNamed:[arrImage objectAtIndex:indexPath.row]];
[self.view addSubview:recipeImageView];
cell.backgroundColor=[UIColor colorWithPatternImage:[UIImage imageNamed:[arrImage objectAtIndex:indexPath.row]]];
return cell;
}
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
UICollectionViewCell* cell=[self.collectionView cellForItemAtIndexPath:indexPath];
cell.contentView.backgroundColor = [[UIColor yellowColor] colorWithAlphaComponent:0.15];
}
- (void)collectionView:(UICollectionView *)collectionView didDeselectItemAtIndexPath:(NSIndexPath *)indexPath
{
UICollectionViewCell* cell=[self.collectionView cellForItemAtIndexPath:indexPath];
UIImageView *recipeImageView = (UIImageView *)[cell viewWithTag:100];
recipeImageView.image = [UIImage imageNamed:[arrImage objectAtIndex:indexPath.row]];
}