如何在我的应用程序中使用自定义UICollectionViewCell时停止内存警告

时间:2014-04-05 09:10:29

标签: ios objective-c cocoa-touch uicollectionview uicollectionviewcell

我可能做错了什么?我使用parse.com信息中心上传了一些jpeg图片,并设置了一些其他数据,如标题和价格。在我的设备上滚动浏览我的collectionView时,它会起伏不定,最终崩溃。

以下是代码:

- (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;

}

我非常感觉这与我的照片有关。我希望有人可以帮我解决这个问题。这是我第一次处理集合视图。

感谢您的时间。 亲切的问候。

1 个答案:

答案 0 :(得分:1)

您的图片大小是什么导致您的收藏夹视图中的内存问题和不连贯的滚动。调整图像大小,将重新调整大小的图像添加到集合视图

//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