Wednesday 6 April 2011

UIImageView

Creating a UIImageVIew with the exact size of an image


create the UIImageView like this:
UIImageView *aImageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"yourImage.png"]];

So the ImageView has the size of the image.

Put UIImageView on the background of the layout.

The simplest way to send image in background layout is
[yourSubView.superview sendSubviewToBack:yourSubView];



Add TextView in UIImageView


Easiest way is when you create imageView set userInteractionEnabled = YES; now create textView or TextField addsubview for imageView.

UIImageView *imgView = [[UIImageView alloc] initWithFrame:CGRectMake(10, 10, 100, 50)]
imgView.image = [UIImage imageNamed:@">

imgView.userInterfaceEnabled = YES;

UITextView *textView = [[UITextView alloc] initWithFrame:CGRectZero];
textView.scrollEnabled = NO;
textView.userInteractionEnabled = NO;
textView.text = content;
[textView setFrame:CGRectMake(0.0, 0.0, imgView.frame.size.width, textView.contentSize.height)];
[imgView addSubview:textView];

[textView release];

[imgView release];



showing Percentage Chart using Imageview

Trying to place an imageView on another ImageView
UIImageView *backgroundImageView = (UIImageView *)[self viewWithTag:kBckGndImag];

if(!backgroundImageView){
UIImage *imageName = [UIImage imageNamed:kpointsChartBig];
backgroundImageView = [[UIImageView alloc] initWithFrame:CGRectMake(15, 15, imageName.size.width, imageName.size.height)];
backgroundImageView.image=imageName;
[backgroundImageView setTag:kBckGndImag];
[pointImageView addSubview:backgroundImageView];
[backgroundImageView release];
}

UIImageView *foregroundImageView = (UIImageView *)[self viewWithTag:kForGndImage];
if(!foregroundImageView){
foregroundImageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:kpointsColoredChartBig]];
foregroundImageView.contentMode = UIViewContentModeLeft;
foregroundImageView.clipsToBounds = YES;
[pointImageView addSubview:foregroundImageView];
[foregroundImageView release];
}

[foregroundImageView setFrame:CGRectake(0,0,/*some percent*/50,foregroundImageView.frame.size.height)];

Finally it looks like this:



 Imageview working as UIButton


- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{

UITouch *touch = [[event allTouches] anyObject];
if (CGRectContainsPoint([img frame], [touch locationInView:self.view])) {

img.alpha = 0.5;
[UIView beginAnimations:@"frame" context:nil];
[UIView setAnimationDuration:1.0];
[UIView setAnimationDelegate:self];
[UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];
img.alpha = 1.0;

[UIView commitAnimations];
NSLog(@"Image Touched");
}
}


Adding an image to a cell as background Image in a UITableView


When adding an Image to a cell is simple, but handling memory is the big issue in iPhone.


static NSString *iden = @"Identifier";

CustomEventCell *cell = (CustomEventCell *)[tableView dequeueReusableCellWithIdentifier:iden];

if(cell == nil) {
cell = [[[CustomEventCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:iden ] autorelease];

UIImage *bgImage = [Utilities getImageWithImageName:kEventCell];//Image Name
//[UIImage ImageName:Image];

UIImageView *bgView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, bgImage.size.width, bgImage.size.height)];

[bgView setImage:bgImage];

if(bgImage)
[bgImage release];

[cell.contentView addSubview:bgView];

[bgView release];

}


In the above code i have used generic class to fetch image as getImageWithImageName you can try using
[UIImage ImageName:Image];.


UIImageView AnimationImages Memory Leak Mistry Solved 


We all know that to do animation using UIImageView, we load images in the NSArrayanimationImages property of UIImageView and then call function startAnimating.

But when you try to repeat animation by loading again and again with different images using this method, you start experiencing Crash on iPhone device with status 101 at Xcode debug. Again, we all know why it crashed, just because of no more memory available for our app in the iPhone device.
But where did our memory went. Here is the answer for that. Every time when we set NSArray to animationImages, the whole data gets copied to it because animationImagesUIImageView. And if we try to again set same animationImages with some other NSArray, we try to nil it first then set with new array. But here we are wrong, when we nil this property, the real time memory decreases but the virtual memory
So instead nil we should call release method of this property which will force it to get freed from the virtual memory too. One more thing you should remember, don’t call release method of this property or don’t try to nil this property in dealloc method where your UIImageView’s reference is released, otherwise either you will get Bad Parameter warning or again virtual memory will not get freed. and then set it to is copy property of doesn’t decrease because garbage collector is not there .

UIImageView *imgView;

NSArray *firstArray, *secondArray;

firstArray= [NSArray arrayWithObjects:[UIImage imageWithContentOfFile: [[NSBundle mainBundle] pathForResource:@”1″ ofType:@”png”]],
[UIImage imageWithContentOfFile: NSBundle mainBundle] pathForResource:@”2″ ofType:@”png”]],
[UIImage imageWithContentOfFile:[[NSBundle mainBundle] pathForResource:@”3″ ofType:@”png”]], nil];

secondArray=[NSArray arrayWithObjects:[UIImage imageWithContentOfFile:[[NSBundle mainBundle] pathForResource:@”11″ ofType:@”png”]],
[UIImage imageWithContentOfFile:[[NSBundle mainBundle] pathForResource:@”22″ ofType:@”png”]],
[UIImage imageWithContentOfFile:[[NSBundle mainBundle] pathForResource:@”33″ ofType:@”png”]], nil];

imgView=[[UIImageView alloc] initWithFrame:CGRectMake(0,0,320,480)];
[imgView setAnimationImages:firstArray];
[imgView setAnimationDuration:1];
[imgView setAnimationRepeatCount=0];
[imgView startAnimating];
[self addSubview:imgView];

After Some time, some where in your code u want to change the animation, then you should do as follows

[imgView.animationImages release];
[imgView setAnimationImages:secondArray];
[imgView setAnimationDuration:1];
[imgView setAnimationRepeatCount=0];
[imgView startAnimating];

And At the dealloc method you should do as follows:

[firstArray release];
firstArray=nil;

[secondArray release];
secondArray=nil;

[imgView removeFromSuperView];//If you not already removed this from super view.
[imgView release];
imgView=nil;

So, if you try this you will definitely gain your virtual memory and you app will never crash due to low memory. Any suggestions are always appreiciated.

Changing UIView’s Coordinate System 


Some times in our app we want to change the coordinate system of UIView, mostly when we change the orientation of iPhone like Landscape Right or left, So that time, coordinate system as well as appearance of UIView should also change, so here is the code to do that…

application.statusBarHidden=YES;

[application setStatusBarOrientation:UIInterfaceOrientationLandscapeRight animated:YES];

myUIView = [[MyUIView alloc] initWithFrame:CGRectMake(0,0,480,320)]; //as now width and height is changed

[window addSubview:myUIView];

CGAffineTransform transform=CGAffineTransformIdentity;
transform=CGAffineTransformRotate(transform, (90.0f*22.0f)/(180.0f*7.0f));
transform=CGAffineTransformTranslate(transform, 80, 80);
[myUIView setTransform:transform];

Now all your subview’s of myUIView will also get transformed. and you will get CGPoint’s of new coordinate system on touch events.

No comments:

Post a Comment