Pages

Wednesday, September 25, 2013

Align UIImageView Center dynamically in xCode

Suppose we have an UIImageView with name imageView and have to set this in center dynamically then we can follow the below step

[imageView setImage:[UIImage imageNamed:@"imageName.png"]];
imageView.autoresizingMask = UIViewAutoresizingNone;

imageView.contentMode = UIViewContentModeCenter;

Monday, September 23, 2013

CREATION OF IMAGE VIEW IN CIRCLE

Creation of image like below.
Image 
1. Take an UIImageView in .xib or storyboard file.
2. Link that name as you want. (Here i have taken imageView_AuthorPhoto)
Now code the below for make that in circular
imageView_AuthorPhoto.layer.masksToBounds = YES;
imageView_AuthorPhoto.layer.cornerRadius = imageView_AuthorPhoto.bounds.size.width/2;
imageView_AuthorPhoto.layer.borderWidth = 1.0;
imageView_AuthorPhoto.layer.borderColor =[UIColor colorWithRed:13/255 green:70/255 blue:131/255 alpha:1.0].CGColor;

And Set Image Accordingly
[imageView_AuthorPhoto  setImageWithURL:[NSURL   URLWithString:@"imageurl"] placeholderImage: useCache:<#(BOOL)#>];

Wednesday, September 18, 2013

Crop Image from an Image in Objective C

Here is the code to cut a piece from an Image or say UIImage

+ (UIImage *)cropImage:(UIImage *)image withArea:(CGRect)rect andScale:(int) scale{
    rect = CGRectMake(rect.origin.x*scale,
                      rect.origin.y*scale,
                      rect.size.width*scale,
                      rect.size.height*scale);
    
    CGImageRef imageRef = CGImageCreateWithImageInRect([image CGImage], rect);
    UIImage *img = [UIImage imageWithCGImage:imageRef
                                       scale:scale
                                 orientation:image.imageOrientation];
    CGImageRelease(imageRef);
    return img;
}

UITableView update Super View from Its Cell

I fought with UITable view and found that there should be some solution to update the Super view according to the selected cell from UITableView.

So i found the following Solution

Like we have added cells in UITableView like as below

NSArray *relatedContentArray = [[NSArray alloc]initWithArray:[self.somearray allObjects]];

RelatedContentTableView *relatedContentTableView = [[RelatedContentTableView alloc]initWithFrame:CGRectMake(0.0f, 88.0f, 280.0f, 460)];

relatedContentTableView.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleTopMargin | UIViewAutoresizingFlexibleBottomMargin | UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth;

relatedContentTableView.dataArray = self.relatedContentArray;
self.relatedContentTableView.selectedContentType = IndiewireRelatedContentFilms;
    

[self.relatedContentTableView.tableView reloadData];

Now we have to get the selected Cell in super view, So here is the solution of this

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    Article *aRelatedArticle = [self.dataArray objectAtIndex:indexPath.row];
    // article is the Varibale in SuperView Class and reloadSubviewsdata is function in Super View. TypeCasting should be like below
    ((ArticleDetailsView *)self.superview.superview).article = aRelatedArticle;

    [ ((ArticleDetailsView *)self.superview.superview) reloadSubviewsData];
}

Friday, September 13, 2013

Load Image from different Sources

Load Image from External URL

NSData *data = [NSData dataWithContentsOfURL:[NSURL URLWithString:@"http://someimageurl/imagename.jpg"]];
    
    
UIImage *image = [[UIImage alloc] initWithData:data];
    
Load Image from Project Itself
    

UIImage *image = [UIImage imageNamed:@"Disp1.jpg"];

Thursday, September 12, 2013

Random Number Array in Objective C

Hi, i think this is a common and basic requirement for every developer in each field, to get the Random numbers in Array. I have made a function to simplify this logic in Objective C.
So here is the code if this
-(void) checkExistance:(int) max
{
    int value = [self randomNumberFrom:0 to:max];//Modify according to minimum /maximum value in Array
    int i = 0;
    for (i = 0; i < [self.numbers count]; i++)
        if ([[self.numbers objectAtIndex:i] integerValue] == value)
            break;
    
    if (i == [self.numbers count])
        [self.numbers addObject:[NSNumber numberWithInt:value]];
    if ([self.numbers count] != max)
        [self checkExistance:max];
}
- (int) randomNumberFrom: (int) minValue to: (int) maxValue
{
    int rangeValue = maxValue - minValue;
    return (arc4random() % rangeValue) + minValue;

}

You just need to call checkExistance method with passing a maximum value in this. You can modify your code for minimum and maximum as well in this. 

Now just call

[self checkExistance:6];

and you will see the result by following

for(int j = 0;j<self.numbers.count;j++)
{
    NSLog(@" %@", [self.numbers objectAtIndex:j]);

}

Monday, September 9, 2013

Attach Multiple View Controllers in a Single Storyboard


I have taken 2 UIViewControllers in a single StoryBoard like in above pic.

First View Controller (with White background in above pic) is linked with ViewController.h and Second View Controller (with Red color background) is linked with SecondVCViewController.h

Linking is done by Selecting Identity Inspector as in below pic



               ^                                                                                                 ^
For First View Controller                                                  For Second View Controller

Note: Mention Other Storyboard ID(s) in Linkage, so that linkage name could identified by below code.

Now define Button's IBAction in its respected class file.

1. Now for First View Controller action


- (IBAction)onTapSecondViewAppearBtn:(id)sender {
    SecondVCViewController *svc = [self.storyboard instantiateViewControllerWithIdentifier:@"SecondVCViewController"];
    [self presentViewController:svc animated:YES completion:nil];

}

2. Now for Second View Controller action, To remove the current View

- (IBAction)onTapFirstViewBtn:(id)sender {
    [self dismissViewControllerAnimated:YES completion:nil];
}

Wednesday, September 4, 2013

Embed Font in Objective C


Here are the Steps to embed font in Objective C

Step 1 -

Put font file inside project file and add that font in project, like in attached top Image - marked with Red square

Step 2 -

Add font in ProjectName-info.plist
By Adding "Fonts provided by application" in item 0 add the name as "FontFileName".

Step 3 -

Add following code inside some comman place of project.

#define BebasNeueFont(_size_)\
[UIFont fontWithName:@"BebasNeue" size:_size_]

Step 4 -

Now Apply font to the label or required place as follow

self.embededFont.font = BebasNeueFont(21);

Result will be shown as below