即时通讯使用Xcode 4.6.1,iOS SDK 6.1,ARC和Storyboard,测试设备iphone 4S运行6.1.3
我在配置包含图片的collectionviewcell时有点麻烦,有些事情是当我尝试在单元格上添加一些自定义图片时,内存只是直接跳过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
然后我使用此代码在集合视图上加载图像
--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我可以使用任何代码。
--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张图像?也许我将它们保存错误或在集合视图中添加错误?谢谢。
答案 0 :(得分:1)
缩小方法中的图像大小
-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView
cellForItemAtIndexPath:(NSIndexPath *)indexPath
您可以使用UIImage
可用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/
中加入以下文件
- 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;
}
希望它有所帮助。