Memory UICollectionView单元内存上的图像警告

Memory UICollectionView单元内存上的图像警告,memory,uiimage,uicollectionviewcell,Memory,Uiimage,Uicollectionviewcell,im使用Xcode 4.6.1、iOS SDK 6.1、ARC和故事板,测试运行6.1.3的iphone 4S 我在配置包含图片的collectionviewcell时遇到了一点问题,当我尝试在cell上添加一些自定义图片时,每个图片的内存直接增加30-40mb,图片直接在设备摄像头上拍摄并保存在文档目录中,也许您可以帮我解决这个问题 以下是我使用的代码: --the code to load the images path on an array imagesArray -(void)

im使用Xcode 4.6.1、iOS SDK 6.1、ARC和故事板,测试运行6.1.3的iphone 4S

我在配置包含图片的collectionviewcell时遇到了一点问题,当我尝试在cell上添加一些自定义图片时,每个图片的内存直接增加30-40mb,图片直接在设备摄像头上拍摄并保存在文档目录中,也许您可以帮我解决这个问题

以下是我使用的代码:

    --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
这是我用来从相机或相机辊保存图像的代码,我必须说我不在乎它是PNG还是JPG,我可以使用任何

--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个图像?可能是我保存错误或在集合视图中添加错误?谢谢。

缩小方法中的图像大小

-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView
         cellForItemAtIndexPath:(NSIndexPath *)indexPath
您可以使用可用的
UIImage
类别

使用该方法

- (UIImage *)thumbnailImage:(NSInteger)thumbnailSize
      transparentBorder:(NSUInteger)borderSize
           cornerRadius:(NSUInteger)cornerRadius
   interpolationQuality:(CGInterpolationQuality)quality;
编辑: 1.从url中包括以下文件

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

希望有帮助。

缩小方法中的图像大小

-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView
         cellForItemAtIndexPath:(NSIndexPath *)indexPath
您可以使用可用的
UIImage
类别

使用该方法

- (UIImage *)thumbnailImage:(NSInteger)thumbnailSize
      transparentBorder:(NSUInteger)borderSize
           cornerRadius:(NSUInteger)cornerRadius
   interpolationQuality:(CGInterpolationQuality)quality;
编辑: 1.从url中包括以下文件

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

希望能有所帮助。

你能更具体地说明我如何使用这种方法吗?我已经尝试过了,而且我在这方面有点新手,但是我注意到我的记忆在增长,而且从未减少,我可以回到“家长”控制器,再次加入这个视图,并且记忆不断积累,记忆从未释放,我做错了什么吗?哇,这确实帮了我很大的忙,但是我可以问你一些与图像无关的事情吗?一旦对象超出范围,或者当对象被收集到垃圾箱时,圆弧会移除对象?我很难确定何时收集对象,因为我仍然可以从上一个视图中看到对象。这个代码到底是做什么的?委托=(AppDelegate*)[[UIApplication sharedApplication]委托];更新对于每个询问为什么会发生这种情况的人,我发现了设备直接从相机保存图像的方式,它做了一些令人讨厌的事情,当它解压缩图像时,每次都会出现内存泄漏,内存不是空闲的,所以我只是用以前的库重新编译原始大小的图像,一切都很顺利。你能更具体地告诉我如何使用这种方法吗?我已经尝试过了,我在这方面有点新,但是我注意到我的内存在增长,而且从不减少,我可以回到“父”控制器,再次加入这个视图,内存不断积累,内存从未释放,我做错了什么吗?哇,这确实帮了我很多忙,但是我可以问你一些与图像无关的事情吗,一旦对象超出范围,或者当对象被收集到垃圾箱时,ARC会移除对象?我很难确定何时收集对象,因为我仍然可以从上一个视图中看到对象。这个代码到底是做什么的?委托=(AppDelegate*)[[UIApplication sharedApplication]委托];更新对于每个询问为什么会发生这种情况的人,我发现了设备直接从相机保存图像的方式,它做了一些令人讨厌的事情,当它解压缩图像时,你每次都会出现内存泄漏,内存不是空闲的,所以我只是用以前的库重新编译原始大小的图像,一切都很顺利。