Friday, February 28, 2014

Compressing Photos/UIImage maintaining Aspect Ratio in iPhone App

Normally, the pictures taken from iPhone camera are high resolutions and it ranges from 1-4 MB in size. When our app needs to send these size of images to the server, upload may take long time which  breaks down the overall app's performance. So in this section, I am sharing a short and sweat objective-c method which will compress original image in iPhone , makes image size small and also the aspect ratio is maintained.
As for my test, 2048*1536 (930 kb) size image  is compressed to 600*450 (169 kb) image and 2448*3264 (2.5 mb) size image is compressed to 600*800 (230 kb) image using the following objective-c method:

- (UIImage *)compressImage:(UIImage *)image {
    float actualHeight = image.size.height;
    float actualWidth = image.size.width;
    float maxHeight = 800.0; //new max. height for image
    float maxWidth = 600.0; //new max. width for image
    float imgRatio = actualWidth/actualHeight;
    float maxRatio = maxWidth/maxHeight;
    float compressionQuality = 0.5; //50 percent compression
   
    if (actualHeight > maxHeight || actualWidth > maxWidth){
        if(imgRatio < maxRatio){
            //adjust width according to maxHeight
            imgRatio = maxHeight / actualHeight;
            actualWidth = imgRatio * actualWidth;
            actualHeight = maxHeight;
        }
        else if(imgRatio > maxRatio){
            //adjust height according to maxWidth
            imgRatio = maxWidth / actualWidth;
            actualHeight = imgRatio * actualHeight;
            actualWidth = maxWidth;
        }
        else{
            actualHeight = maxHeight;
            actualWidth = maxWidth;
        }
    }
    NSLog(@"Actual height : %f and Width : %f",actualHeight,actualWidth);
    CGRect rect = CGRectMake(0.0, 0.0, actualWidth, actualHeight);
    UIGraphicsBeginImageContext(rect.size);
    [image drawInRect:rect];
    UIImage *img = UIGraphicsGetImageFromCurrentImageContext();
    NSData *imageData = UIImageJPEGRepresentation(img, compressionQuality);
    UIGraphicsEndImageContext();
   
    return [UIImage imageWithData:imageData];
}

* depending on your compressionQuality , the image size and quality varies, and its value ranger from 0 to 1.

Tuesday, July 16, 2013

Programmatically detecting isDevice iPhone 5 ?


Define global  condition as below in your global classes:

#define IS_IPHONE_5 ( fabs( ( double )[ [ UIScreen mainScreen ] bounds ].size.height - ( double )568 ) < DBL_EPSILON )

Used C functions:
DBL_EPSILON = will give the smallest value that will make a difference when adding with one.  Smallest number such that 1.0 + DBL_EPSILON  != 1.0.
fabs = gives absolute value of x (a negative value becomes positive, positive value is unchanged).

Now simply you can detect iPhone 5 as :
if(IS_IPHONE_5){
            NSLog(@"I am iPhone 5");
}
else {
              NSLog(@"I am not iPhone 5");

}

Monday, July 15, 2013

Auto Search/Auto Complete using UITextField as SearchBar for iOS app


After a long gap, I made a time off  from my regular stuffs to write here few things. This time I choosed a topic  “Auto Search/Auto Complete” ,. You guyz already knew about this feature , if not, then auto search/Auto complete is the process of searching when you type some character in Search field. Auto search simply detects each of the character you type and starts searching. Moreover, When a user is entering data into a text field, it’s often nice to have the ability to auto complete with custom values based on what the user is entering, saving their time.

Auto Search  the name,  always sounds complicated  . Even I always thought doing auto search features in iOS app must be complicated but I was wrong . It is not hard as it sounds, and how I figured it out , I am explaining here with simply using UItextField and its native method.

Here are the step by step guidelines :

1.     First thing you need to create is UITextField which works as a search bar and UITableView for displaying your search results. And another thing is data source (Array/Dictionary) which has a collection of data from where we need to search  based on the name/strings which user types in text field.
2.     You can play with UItableView  in your own way. You simply can hide it first and when starts searching and finds result , you can unhide the table and reload the data. OR simply you can only add table view when search starts and remove it when we are done with the selection.
Here, I am using Hidden property of UItableView.
3.     Create UITextField , define <UITextFieldDelegate> in you .h file

Tuesday, April 23, 2013

Creating Watermarked Images from iOS app

Hello Guyz, In this section i will give you a bunch of code for creating a watermarked images. For example: Images with copyright text on top of it.  It sounds difficult but believe me, I have a really simple solution for you. In this part,  I am only merging  two of the images (one is watermark image and another is the image in which you want to put watermark) and hence making them one. So before we proceed, remember you should have watermark image and main image(picture) in your project resources.

-(UIImage *) generateWatermarkForImage:(UIImage *) mainImg{
      UIImage *backgroundImage = mainImg;
    UIImage *watermarkImage = [UIImage imageNamed:@"watermark.png"]; 

Wednesday, February 27, 2013

NSDocumentsDirectory and iCloud issue :preventing files in a App's Documents Directory being synced to iCloud

Many of you guys use app's  "Documents Directory" to store your files(image,video,pdf,music etc) in iOS app and being an iOS developer, it's obvious that you use documents directory since for each app,its the best way to store your files on the device permanently ,so that your app can  function properly in offline mode too.
However, if you are trying to store large number of files (obvious referring both in quantity and size) in app's documents directory, then i will say STOP & WAIT!!! 

- "STOP" because your app gonna be rejected by Apple because according to Apple's latest iOS Data Storage Guidelines : "Since iCloud backups are performed daily over Wi-Fi for each user’s iOS device, it’s important to ensure the best possible user experience by minimizing the amount of data being stored by your app." In summery i will say there are two points, every files in NSDocumnetsDirectory will be synced to user's iCloud and another is,if file sizes stored in NSDocumnetsDirectory are large then Apple will reject it since that causes the slow down on syncing process as iCloud-device syncs on daily basis.
-"WAIT" because i have a solution for it.
As pointed above if you are going to save files in NSDocumentDirectory then it will be back-up to iCloud,that means any thing you store in documents folder, will be synced to user's iCloud and will be publicly available. So if your files stored in documents directory  makes no sense by being public then you don't have to worry about anything(unless its size is bigger), but if your file  stored there are large in size and  can not be public,or i say if these files are only intended just for the scope of your app then you must have to do extra work to prevent from being synced to iCloud.

There are two solutions for it :

Monday, February 25, 2013

Sharing your app via Facebook and Twitter using Social Framework

Hello Guyz, in this section i will be describing about how we can use new "Social Framework" for sharing our app(tweet+status) from our iOS apps. It's really easy to deal with but, we should keep in mind that Social framework is only available from iOS 6 and later . Here, we will be only focusing on how to share our app , along with how to tweet and post a status from our iOS app.



1. Add "Social.framework" in your project.
2. Include  #import <Social/Social.h> to your .h file
3. Now, in your sharing method :

Tuesday, February 5, 2013

Ad-Hoc Distribution of iOS Apps - Simple Solution

If you are searching for a blog that describes the better way to do a ad hoc distribution of your iOS apps, I simply prefer you to go through : http://www.diawi.com/ 
This is the perfect way of distributing and installing iOS apps on real device. We don't need to sync app  through iTunes and even we do not need any computers, we can simply download it on our device and install it.

Note : Before proceeding, first make .ipa or .app file of your project from Xcode since in this section i am not talking about how to make .ipa/.app file from xcode but i am talking about how to send your apps to the end test users in a simplest and efficient way.

Best of Luck !!!