UICollectionView image on cell memory warning

巧了我就是萌 提交于 2019-11-29 16:55:07

Scale down the image size in the method

-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView
         cellForItemAtIndexPath:(NSIndexPath *)indexPath

You can use the category for UIImage available here

Use the method

- (UIImage *)thumbnailImage:(NSInteger)thumbnailSize
      transparentBorder:(NSUInteger)borderSize
           cornerRadius:(NSUInteger)cornerRadius
   interpolationQuality:(CGInterpolationQuality)quality;

Edit: 1. Include the following files from the url http://vocaro.com/trevor/blog/2009/10/12/resize-a-uiimage-the-right-way/

  • UIImage+Resize.h, UIImage+Resize.m
  • UIImage+RoundedCorner.h, UIImage+RoundedCorner.m
  • UIImage+Alpha.h, UIImage+Alpha.m

2.Import the UIImage+Resize.h.

3.Alter your method

-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView
         cellForItemAtIndexPath:(NSIndexPath *)indexPath

to look like the below

-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    NSLog(@"Entra a las celdas");
    static NSString *identificador=@"celdaImagen";
    celdaImagen *myCell = [collectionView dequeueReusableCellWithReuseIdentifier:identificador forIndexPath:indexPath];
    UIImage* image = [UIImage imageWithContentsOfFile:[imagesArray objectAtIndex:indexPath.item]];

    NSData *imageData = UIImagePNGRepresentation(image); //png o jpg
    NSLog(@"tamano imagen %i",(imageData.length)/1024);

    myCell.imagen.image=[image thumbnailImage:20 // This should the size of the view in collection view. example: myCell width is 20 and height is 20. 
                            transparentBorder:0
                                 cornerRadius:0
                         interpolationQuality:kCGInterpolationMedium];
    return myCell;
}

hope it helps.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!