Вопрос

What could I be doing wrong? I've uploaded a few jpeg image using parse.com dashboard and set some other data such as title and price. When scrolling through my collectionView on my device it's choppy then eventually crashes.

Here is the code:

- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
    return 1;
}


-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return [[self objects] count];
}

-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath object:(PFObject *)object
{
    NSArray *people = [self objects];
    static NSString *CellIdentifier = @"Cell";

    VAGGarmentCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier: CellIdentifier forIndexPath:indexPath];


    PFObject *current;

    current = [people objectAtIndex:indexPath.item];

    PFFile *userImageFile = current[@"image"];

    [userImageFile getDataInBackgroundWithBlock:^(NSData *imageData, NSError *error) {
        UIImage *image = [UIImage imageWithData:imageData];

        [[cell contentView] setContentMode: UIViewContentModeScaleAspectFit];
        [[cell imageView] setImage:image];

    }];

    [[cell title] setText:[current valueForKey:@"title"]];
    [[cell price] setText:[NSString stringWithFormat: @"£%@", [current valueForKey:@"price"]]];

    return cell;

}

I have a strong feeling it's something to do with my images. I hope someone can help me solve this issue. This is the first time I've dealt with collection view.

Thanks for your time. Kind regards.

Это было полезно?

Решение

Your image size is whats causing your memory issues in your collection view and choppy scrolling. Resize your image, add the re sized image to your collection view

//resize image
CGSize destinationSize = CGSizeMake(264,476);
UIGraphicsBeginImageContext(destinationSize);
[image drawInRect:CGRectMake(0,0,destinationSize.width, destinationSize.height)];

//New image
UIImage*newImage = UIGraphicsGetImageFromCurrentImageContext();
                    UIGraphicsEndImageContext();

//Optimise image
NSData *imageDataCompressed = UIImageJPEGRepresentation(newImage, 0.4f);
                    NSLog(@"Image Size %@", NSStringFromCGSize(newImage.size));//log size of image
Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top