Sunday 17 April 2016

Displaying Vector images on iOS Devices

Good day folks,

Actually, it was  avery good step that Apple added Vector feature to their devices. It has a lot of advantages in my point of view:-

  • Decreasing app size: you don't need 3 types of each image to display it for different resolutions.
  • Decreasing images problems: Some times I got images with some problems in @2x only or @3x only.
But the main problem here is the format Apple supports. It does only support Vector PDF which is not widely common and a lot of images online are a SVG instead.

I wanted to get a way to display SVG on my iOS and I passed by many like PocketSVG and SVGKit. But all of them are very heavy and full of bugs.

So, in the solution here, with some online help, I managed to make this very small-light library. Actually you can say it's a small function which is used to display SVG images not on UIImageView but on UIWebView.

The problem I faced was to resize my SVG on that WebView and I solved it by embedding it into an image.

Check my repository here.

Thursday 5 June 2014

Swift Sample code

The following is a sample code for Swift, the new Apple language.
This sample code include

  1. Request and receive from iTunes server.
  2. Parse JSON response and store it.
  3. Supporting OOP.
  4. Supporting delegates & protocols.
  5. Async request to images.
  6. Adding IBAction method.
You can check the source code here and again, thanks to Jameson. Without him this code will never be existed.

Thursday 16 August 2012

How to avoid 0x8badf00d crash

0x8badf00d  is a crash that is happening if one of the three methods takes much more time than it should take (around 5 seconds)

There are two on startup, applicationDidFinishLaunching: and application:didFinishLaunchingWithOptions, and one on shutdown, applicationWillTerminate.
But the most important & frequent one is "application:didFinishLaunchingWithOptions".

To avoid this, simply use the following trick

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    [self.window makeKeyAndVisible];   
    [self.loadingWheel startAnimating];       
   
   
    [self performSelector:@selector(didFinishLaunchingWithOptionsAfterDelay:) withObject:launchOptions afterDelay:0.01];
   
    return YES;
}


- (BOOL)didFinishLaunchingWithOptionsAfterDelay:(NSDictionary *)launchOptions{
   
  //Spend your holiday here :)   
}

As you see, simply call another method after delay & put every line that was in  "application:didFinishLaunchingWithOptions", that solves.

Thursday 26 May 2011

Facebook for iphone [login dialog]

For those developers who downloaded & worked with facebook plug-in with iphone, the login process is by default is like Gowalla. Where safari appeared & u login from there.
To show login dialog do these steps:-
1) Open facebook.m.
2) In function authorizeWithFBAppAuth, hash this block of code:-

if ([device respondsToSelector:@selector(isMultitaskingSupported)] && [device isMultitaskingSupported]) {
if (tryFBAppAuth) {
NSString *fbAppUrl = [FBRequest serializeURL:kFBAppAuthURL params:params];
didOpenOtherApp = [[UIApplication sharedApplication] openURL:[NSURL URLWithString:fbAppUrl]];
}

if (trySafariAuth && !didOpenOtherApp) {
NSString *nextUrl = [NSString stringWithFormat:@"fb%@://authorize", _appId];
[params setValue:nextUrl forKey:@"redirect_uri"];

NSString *fbAppUrl = [FBRequest serializeURL:loginDialogURL params:params];
didOpenOtherApp = [[UIApplication sharedApplication] openURL:[NSURL URLWithString:fbAppUrl]];
}
}

3) Enjoy:))

popovercontroller for iphone not shown

Of course all of u know that uipopovercontroller is not existed for iPhone..it's only for iPad.
So the solution i given thanx to PaulSolt here .
The problem faced me was in UITabBarController. So, the solution to the problem of popover not appeared is :-
1) Open WEPopOverController.m
2) In function presentPopOverFromRect, change the line [keyview addSubview:backgroundView];
to [theView addSubview:backgroundView];
3) enjoy:))

Wednesday 23 February 2011

Hide UITabBarController/UITabBar with animation.

BOOL hiddenTabBar;
UITabBarController *tabBarController;

- (void) hidetabbar {

[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:0.5];

for(UIView *view in tabBarController.view.subviews)
{

if([view isKindOfClass:[UITabBar class]])
{

if (hiddenTabBar) {
[view setFrame:CGRectMake(view.frame.origin.x, 431, view.frame.size.width, view.frame.size.height)];
} else {
[view setFrame:CGRectMake(view.frame.origin.x, 480, view.frame.size.width, view.frame.size.height)];
}
} else {
if (hiddenTabBar) {
[view setFrame:CGRectMake(view.frame.origin.x, view.frame.origin.y, view.frame.size.width, 431)];
} else {
[view setFrame:CGRectMake(view.frame.origin.x, view.frame.origin.y, view.frame.size.width, 480)];
}

}
}

[UIView commitAnimations];

hiddenTabBar = !hiddenTabBar;
}

//Taken from https://sites.google.com/a/injoit.com/knowledge-base/for-developers/graphics/hide-uitabbarcontroller-uitabbar-with-animation

Wednesday 29 December 2010

How to make infinite-like UIScrollView

There are two options:-

1) You want the width of the image = width of the screen i.e 320 px.

The basic idea is that you store the images in that order "Assuming you have 3 images":-
3 1 2 3 1
in that order, when you reach to the pre-last image "second 3" and scroll again, you see the image number 1 "second 1" & in a moment, the scroll view will scroll to the offset of the first one.
It's the same image, so the user will not detect any flicker.

2) You want the width of the images <>

You do a similar idea, assume also you have 3 images, the order of the images in the circular list will be:- "like here http://allrecipes.com/features/more/iphone.aspx"
1 2 3 1 2 3 1 2 3

by doing so, when you are in image "third 1" you move the offset of the "second 1",
so, the same sequence of images will be the same!!

here you are a very simple working sample code:-

#define WIDTH_OF_IMAGE 63

#define HEIGHT_OF_IMAGE 70



- (void)viewDidLoad {

[super viewDidLoad];

scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 341,320,HEIGHT_OF_IMAGE)];

scrollView.showsHorizontalScrollIndicator=NO;

[self.view addSubview:scrollView];

[self setupScrollView];

}


-(void) setupScrollView{

scrollView.bounces = YES;

//scrollView.pagingEnabled = YES;

scrollView.delegate = self;

scrollView.userInteractionEnabled = YES;

slideImages = [[NSMutableArray alloc] init];

[slideImages addObject:@"activity_01.png"];

[slideImages addObject:@"activity_02.png"];

[slideImages addObject:@"activity_03.png"];

[slideImages addObject:@"activity_04.png"];

[slideImages addObject:@"activity_05.png"];

[slideImages addObject:@"activity_06.png"];

[slideImages addObject:@"activity_07.png"];

[slideImages addObject:@"activity_08.png"];

[slideImages addObject:@"activity_09.png"];

// add all of the images to the scroll view

for (int i =0; i < [slideImages count]; i++) {

[self addImageWithName:[slideImages objectAtIndex:i] atPosition:i];

}

for (int i =0; i < [slideImages count]; i++) {

[self addImageWithName:[slideImages objectAtIndex:i] atPosition:i+[slideImages count]];

}

for (int i =0; i < [slideImages count]; i++) {

[self addImageWithName:[slideImages objectAtIndex:i] atPosition:i+2*[slideImages count]];

}


scrollView.contentSize = CGSizeMake((3*[slideImages count])*WIDTH_OF_IMAGE, HEIGHT_OF_IMAGE);

[scrollView scrollRectToVisible:CGRectMake(WIDTH_OF_IMAGE,0,WIDTH_OF_IMAGE,HEIGHT_OF_IMAGE) animated:NO];

}



- (void)addImageWithName:(NSString*)imageString atPosition:(int)position {

// add image to scroll view

UIImage *image = [UIImage imageNamed:imageString];

UIImageView *imageView = [[UIImageView alloc] initWithImage:image];

imageView.frame = CGRectMake(position*(WIDTH_OF_IMAGE+LEFT_EDGE_OFSET), 0, WIDTH_OF_IMAGE, HEIGHT_OF_IMAGE);

[scrollView addSubview:imageView];

[imageView release];

}


//Moving the scroll view to the desired offset

- (void)scrollViewDidScroll:(UIScrollView *)scrollView1{

if (scrollView1.contentOffset.x <=([slideImages count]-1)*WIDTH_OF_IMAGE) {

[scrollView setContentOffset:CGPointMake(([slideImages count]+([slideImages count]-1))*WIDTH_OF_IMAGE, 0)];

}

else if (scrollView1.contentOffset.x >=(2*([slideImages count]))*WIDTH_OF_IMAGE) {

[scrollView setContentOffset:CGPointMake(([slideImages count])*WIDTH_OF_IMAGE, 0)];

}

}



- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView1{


if (scrollView1.contentOffset.x <=([slideImages count]-1)*WIDTH_OF_IMAGE) {

[scrollView setContentOffset:CGPointMake(([slideImages count]+([slideImages count]-1))*WIDTH_OF_IMAGE, 0)];

}

else if (scrollView1.contentOffset.x >=(2*([slideImages count]))*WIDTH_OF_IMAGE) {

[scrollView setContentOffset:CGPointMake(([slideImages count])*WIDTH_OF_IMAGE, 0)];

}

}


Ready for questions!!!