Monday, July 30, 2012

Best Way To Acess Array Containing Dictionary

While development many times it happens that we get data in dictionary trap in a array as below

Sample Data:

        ({
        0 = 31555800;
        1 = "0.3959247911412359168";
    },
        {
        0 = 63091800;
        1 = "0.4162550244325347328";
    },
        {
        0 = 94714200;
        1 = "0.5971193253467844608";
    },
        {
        0 = 126250200;
        1 = "1.5545692780580073472";
    })



let suppose we have more then 100 of data then the problem we face when we have to find:
 1. largest value on any key. (on key "1" is 1.5545692780580073472)
 2. Smallest value on any key. (on key "1" is 0.3959247911412359168)
 3. fetch all the values at any key to one array.
 4. fetch all the unique value at any key to one array.
 5. short the array according to any key value.


To Find max value at key "1" :
float max = [[dataArray valueForKeyPath:@"@max.1"] floatValue];

To Find min value at key "1" :
 float min = [[dataArray valueForKeyPath:@"@min.1"] floatValue];

To Fetch all the values at key "1":
 NSArray *allOne=[dataArray  valueForKeyPath:@"1"];

To find unique value at key "1":
NSArray *uniqueData=[dataArray  valueForKeyPath:@"@distinctUnionOfObjects.1"];


Shorting array according to key "1" values:

NSSortDescriptor *newDiscriptor = [[NSSortDescriptor alloc] initWithKey:@"1"  ascending:YES];
     
NSArray *descriptors = [NSArray arrayWithObjects:newDiscriptor, nil];        

NSArray *sortedArray = [dataArray sortedArrayUsingDescriptors:descriptors];

--------------------------------------------------------------------------------------------------------
I will post more if i find any other thing regarding array and dictionary :)

Sunday, June 3, 2012

image compression by size by maintaining original height and width

While we develop application for iphone we face 
a serious problem of memory because we use images 
of large size, for better quality, but some time 
it is required to compress images (from MB to KB) 
but without change in height and width 
for sending it to server or other purpose.
 
There is code in SDK to compress image by height 
and width but but I would like to compress the images 
by size to a fixed size (200 KB) only and keep the 
original height and width. The scale factor in 
JPEGRepresentation does not represent the size and 
only compression quality. 
 
How can we achieve this (compress to a fixed size) 
without using any third party library?
 

example code that will attempt to compress an image 
for you so that it doesn't exceed either a max 
compression or maximum file size.
 

UIImage *existingImage=[UIImage imageNamed:
@"ExistingImg.jpg"];

CGFloat compression = 0.9f;
CGFloat maxCompression = 0.1f;
 
int maxFileSize = existingImage.size.height*
existingImage.size.width;
 
NSData *imageData = UIImageJPEGRepresentation(existingImage,
 compression);
 
while ([imageData length] > maxFileSize && 
compression > maxCompression)
{
    compression -= 0.1;
    imageData = UIImageJPEGRepresentation(existingImage, 
compression);
}



One way to do it, is to re-compress the file in a 
loop, until you find the desired size.  You could 
first find height and width, and guess the compression 
factor (larger image more compression) then after you 
compress it, check the size, and split the difference 
again.