1

Xcode 4.6.1、iOS SDK 6.1、ARC、およびストーリーボードを使用して、6.1.3 を実行しているデバイス iPhone 4S をテストしています。

写真を含む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 つ以上の画像を表示できるカメラ ロールのようなアプリケーションを実行するための最良のアプローチは何ですか? 多分私はそれらを間違って保存しているか、コレクションビューに間違って追加していますか? ありがとうございました。

4

1 に答える 1

1

メソッドで画像サイズを縮小する

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

ここUIImageで利用可能なカテゴリを使用できます

メソッドを使用する

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

編集: 1. 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.インポートし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;
}

それが役に立てば幸い。

于 2013-06-18T06:24:47.987 に答える