UICollectionView单元格内存上的图像警告 [英] UICollectionView image on cell memory warning

查看:94
本文介绍了UICollectionView单元格内存上的图像警告的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用Xcode 4.6.1,iOS SDK 6.1,ARC和Storyboard进行即时测试,测试运行6.1.3的设备iphone 4S

我在配置包含图片的collectionviewcell时遇到了一些麻烦,当我尝试在单元格上添加一些自定义图片时,内存只是每张图片直接跳了30-40mb,图片直接在设备摄像头并保存在文档目录中,也许您可​​以帮助我.

I'm having a little trouble configuring a collectionviewcell containing pictures, there thing is when i try to add some custom pictures on the cell, the memory just jump directly by 30-40mb each picture, the picture are taking directly on the device camera and saved on documents directory, maybe you can help me with this.

这是我使用的代码:

    --the code to load the images path on an array imagesArray
-(void) llenarArregloImagenes
{
imagesArray=[[NSMutableArray alloc]init];
NSFileManager *fileMgr=[[NSFileManager alloc]init];
NSError * error = nil;
NSString *documentsDirectory = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)     lastObject];
//vemos cuantos objetos con la extension .jpg hay
NSArray *contenidoDirectorio=[fileMgr contentsOfDirectoryAtPath:documentsDirectory error:&error];
//FILTER THE JPG FILES
contenidoDirectorio = [contenidoDirectorio filteredArrayUsingPredicate:[NSPredicate predicateWithFormat:@"pathExtension ==[c] %@", @"jpg"]];
//para meter en el arrelo las imagenes
int indice=0;
if([contenidoDirectorio count]>0)
{
for(indice=0;indice<=[contenidoDirectorio count]-1;indice++)
{
    NSString* path = [documentsDirectory stringByAppendingPathComponent: [NSString stringWithString:[contenidoDirectorio objectAtIndex:indice]]];
    [imagesArray addObject:path];
}
}

    --end loading images

然后我使用此代码将图像加载到收藏夹视图中

then i use this code to load the images on the collection view

--collection view code to add image to cell
-(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]];
//myCell.imagen.image = [imagesArray objectAtIndex:indexPath.item];
//comprimir la imagen
//double compressionRatio=1;
//NSData *imgData=UIImageJPEGRepresentation(image,compressionRatio);
//NSLog(@"tamano de la imagen %i",(imgData.length)/1024);
//UIImage *imagenRedimensionada=[[UIImage alloc] initWithData:imgData];
//
NSData *imageData = UIImagePNGRepresentation(image); //png o jpg
NSLog(@"tamano imagen %i",(imageData.length)/1024);
myCell.imagen.image=image;
return myCell;
}
--end of loading images

这是我用来从相机或相机胶卷保存图像的代码,我必须说我不在乎是PNG还是JPG,我可以使用任何代码.

this is the code i use to save the image from the camera or from the camera roll, i must say i dont care if it is PNG or JPG i can use any.

--code to save image on documents file 
-(void)imagePickerController:(UIImagePickerController *)picker
didFinishPickingMediaWithInfo:(NSDictionary *)info
{
NSString *mediaType = info[UIImagePickerControllerMediaType];

[self dismissViewControllerAnimated:YES completion:nil];

if ([mediaType isEqualToString:(NSString *)kUTTypeImage]) {
    UIImage *image = info[UIImagePickerControllerOriginalImage];
        NSData *imageData = UIImageJPEGRepresentation(image,0.009); //png o jpg
        NSLog(@"tamano imagen %i",(imageData.length)/1024);
        NSFileManager *fileMgr=[[NSFileManager alloc]init];
        NSError * error = nil;
        NSString *documentsDirectory = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)     lastObject];
        //vemos cuantos objetos con la extension .png hay
        NSArray *contenidoDirectorio=[fileMgr contentsOfDirectoryAtPath:documentsDirectory error:&error];
        //contenidoDirectorio = [contenidoDirectorio filteredArrayUsingPredicate:[NSPredicate predicateWithFormat:@"pathExtension ==[c] %@", @"mov"]];
        NSString *nombreArchivo=[NSString stringWithFormat:@"imagen%i.jpg",[contenidoDirectorio count]] ;
        NSString *path = [documentsDirectory stringByAppendingPathComponent:nombreArchivo];
        //mostramos el contenido del archivo
        NSLog(@"Documents directory: %@", [fileMgr contentsOfDirectoryAtPath:documentsDirectory error:&error]);
        //guardamos el archivo
        [imageData writeToFile:path options:NSDataWritingAtomic error:&error];
        //volvemos a cargar los datos del collectionview
        [self llenarArregloImagenes];
        //metemos la ruta completa de la imagen
        //double compressionRatio=0.1;
        //NSData *imgData=UIImageJPEGRepresentation(image,compressionRatio);
        //UIImage *imagenRedimensionada=[[UIImage alloc] initWithData:imgData];
        //
        //[imagesArray addObject:imagenRedimensionada];
        [self.collectionView reloadData];
        if (error != nil)
        {
            NSLog(@"Error: %@", error);
            return;
        }
}
else if ([mediaType isEqualToString:(NSString *)kUTTypeMovie])
{
    // Code here to support video if enabled
}
}
--end of code to save image

像胶卷一样,可以在其中看到6张以上图像的最佳方法是什么?也许我是在错误地保存它们或将错误添加到集合视图?谢谢.

what is the best aproach to do an aplication like the camera roll, where i can see more than 6 images? maybe i'm saving them wrong or adding wrong to the collection view? thank you.

推荐答案

缩小方法中的图片大小

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

您可以将类别用于UIImage可用的此处

You can use the category for UIImage available here

使用方法

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

修改: 1.包含来自网址 http://vocaro.com/trevor/blog/2009/10/12/resize-a-uiimage-the-right-way/

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
  • UIImage+Resize.h, UIImage+Resize.m
  • UIImage+RoundedCorner.h, UIImage+RoundedCorner.m
  • UIImage+Alpha.h, UIImage+Alpha.m

2.导入UIImage+Resize.h.

3.更改您的方法

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

如下所示

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

希望有帮助.

这篇关于UICollectionView单元格内存上的图像警告的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆