0

I need to find total size of all images inside photo library in iPhone. I have using Assets Library framework and successfully find number of images. However images are of smaller size and converting them into MB giving me wrong result. Below is the source code.

 ALAssetsLibrary *library = [[ALAssetsLibrary alloc] init];
[library enumerateGroupsWithTypes:ALAssetsGroupSavedPhotos usingBlock:^(ALAssetsGroup *group, BOOL *stop) {

    [group enumerateAssetsUsingBlock:^(ALAsset *asset, NSUInteger index, BOOL *stop)
     {
         if ([[asset valueForProperty:ALAssetPropertyType] isEqualToString:ALAssetTypePhoto]) {

             UIImage *image = [UIImage imageWithCGImage:asset.defaultRepresentation.fullResolutionImage];
             [self performSelectorOnMainThread:@selector(usePhotolibraryimage:) withObject:image waitUntilDone:NO];
         }
     }];
}
 failureBlock:^(NSError *error) {
     NSLog(@"%@",error.description);
 }];


- (void)usePhotolibraryimage:(UIImage *)myImage{

    //Do your all UI related and all stuff here

    NSData *imageData = UIImageJPEGRepresentation(myImage, 0.5);

    int imageSize = imageData.length/1024;
    totalImageSize = totalImageSize+imageSize;
}

However while converting image into MB its showing me wrong count of all images, Please let me know what i am doing wrong.

user741076
  • 63
  • 1
  • 3
  • 9

2 Answers2

2

Use asset.defaultRepresentation.size

Returns the size in bytes of the file for the representation.

  • (long long)size

Apple doc: link

torip3ng
  • 585
  • 2
  • 7
  • NSData length Returns the number of bytes contained in the receiver. - (NSUInteger)length https://developer.apple.com/library/mac/#documentation/Cocoa/Reference/Foundation/Classes/NSData_Class/Reference/Reference.html – Rajneesh071 Jul 02 '13 at 06:55
0

In your code int imageSize = imageData.length/1024; This will show size in KB.
If you want it in MB then divide it again with 1024.

And make totalImageSize of type NSInteger.

Here is the link for length of NSData

Rajneesh071
  • 30,846
  • 15
  • 61
  • 74