annotationView override drawRect, image with alpha - iphone

Hello i want to override drawrect in my custom annotationView, so when i write
[[_mapView viewForAnnotation:annotation] setNeedsDisplay];
my annotation view will be redrawn and i wouldn't have to remove the annotation and add it again.
here is my drawRect
- (void)drawRect:(CGRect)rect {
UIImage* theImage = nil;
if( _pinType == T_UNKNOWN ) theImage = [UIImage imageNamed:#"imgU.png"];
else theImage = [UIImage imageNamed:#"imgK.png"];
[theImage drawInRect:rect];
}
The problem is that my images are with alpha and the alpha part is black.
So maybe anyone knows the solution or some suggestions to this?
I've read a lot of post about this, also using core graphics, but didn't find the solution..
Thanks in advance!

Do you want this view to be partially transparent and display things under it? If so, use [self setOpaque:NO]. An opaque view's drawRect is responsible for drawing every pixel in the rectangle with a fully opaque color.

This function will be work correct for iOS 5.0. When you will use iOS version < 5.0, you'll got alpha part as black.
To try use another method for draw your images. I don't know for what you use this code. To try use:
UIImageView *image = [[UIImageView alloc] initWithImage: theImage];
image.opaque = NO;
[self.view addSubview: image];

Related

why the image become zoom while converting view to image?

I have uiview and i want to convert that view to image which is working quite well but when i pressed the home button of iphone and run the app again the image become zoom in .Here is my code to converting view to image.
- (UIImage *)captureView {
CGRect rect = [myview bounds];
UIGraphicsBeginImageContext(rect.size);
CGContextRef context = UIGraphicsGetCurrentContext();
[myview.layer renderInContext:context];
UIImage *img = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return img;
}
checkImage.image = [self captureView];
here this code is not wrong but you used some other object instead of whole view . i don't know but try to use that view bounds in which that whole Image display.. See my answer same like your this answer from this bellow link..
how to capture uiview top uiview
Also try with self.view instead of myView in your code.
add this code instead of your checkImage.image = [self captureView]; line..
UIImageView *imgTempHeader = [[UIImageView alloc]initWithImage:[self captureView]];
imgTempHeader.frame = self.view.frame;
[self.view addSubview:imgTempHeader];
[self.view bringSubviewToFront:imgTempHeader];
You should be creating the context in a way which uses the scale of the device:
UIGraphicsBeginImageContextWithOptions(rect.size, NO, 0);
Then when you display the image in the image view ensure that the content mode is set to prevent scaling.
My experience is with setting up imageViews in IB. I assume checkImage is an instance of UIImageView?
If you were displaying an image in a UIImageView and had only set the view mode to center in interface builder it would appear zoomed in if the image was large.
Have a look at this way to process your image first:
UIImage *newImage = [[UIImage alloc] initWithCGImage:myCGImageRef scale:myZoomSCale orientation:UIImageOrientationDown];
Of course you will need to give it a CGImageRef for your image. Over to you or somebody else for that.

How to assign separate memory spaces of same type of class

Hi my program adds small Images to main view. I have this undo button to remove recently added Image(subView). It works ok when it has all different Images, But when there are two same images it occurs error.
I think this is because it both points the same original png file. But I have no idea how to fix it. Please give me some hint.
add{
UIImage *image = [UIImage imageNamed:[NSString stringWithFormat:#"pah%d",tagNum]];
TouchImageView *touchImageView = [[TouchImageView alloc] initWithFrame:imageRect];
imageCounter++;
touchImageView.tag = imageCounter;
touchImageView.image = image;
touchImageView.center = CGPointMake(160.0, 230.0);
[view addSubview:touchImageView];
}
undo{
[[self.view viewWithTag:imageCounter] removeFromSuperview];
imageCounter--;
}
I doubt that its your problem here but imageNamed: caches the image in memory with an internal caching system. Every time you ask for [UIImage imageNamed:#"foo"] you get the same UIImage instance.
You probably want to be using imageWithContentsOfFile: instead which returns a unique instance of a UIImage.
Try that and see if it makes a difference.
if you just have to remove recently added image...
then each time where you are adding the image store its reference like this - it will work well with ARC...
UIImageView *imageView = touchImageView;
then in your remove Recently added image button click's
for(UIImageView *iV in view.subviews)
{
if(iV == imageView)
{
[iV removeFromSuperView];
}
}
i think it will work ...

iOS 4.3 transparent png in status bar isn't transparent

I'm having some difficulty getting the transparent bits of my png image that I'm using to replace the default status bar to be rendered as transparent in iOS 4.3. At the moment they are coming up black.
Here is the code I'm using to draw my image:
#implementation UINavigationBar (BackgroundImage)
- (void)drawRect:(CGRect)rect
{
UIImage *image = nil;
switch(self.tag)
{
case HeaderBG_Logo:
image = [UIImage imageNamed: #"top_bar_logo.png"];
break;
case HeaderBG_Plain:
image = [UIImage imageNamed: #"top_bar.png"];
break;
}
if(image != nil)
{
[image drawInRect:CGRectMake(0, 0, self.frame.size.width, self.frame.size.height)];
}
}
#end
Here is a side by side comparison of the same app running in the simulator in ios4.3 and ios5 (the image has a drop shadow as its bottom border):
As an aside, the 4.3 background image doesn't seem to be rendered as tall as the 5.0 one either.
I've tried setting the UINavigationBar to opaque / setting its background color to clearColor. None of that worked. :(
Can anyone help?
I ended up using the solution from How to create UINavigationBar drop shadow to add a shadow to the UINavigationBar instead.
Just an hint, for iOS 5 you could use the new appreance property to set the background image:
[[UINavigationBar appearance] setBackgroundImage:[UIImage imageNamed:#"top_bar.png"] forBarMetrics:UIBarMetricsDefault];
This will set the image for all UINavigationBars in the app.
I am not sure to understand if you are concerned by the status bar or by the navigationBar. According to you question it seems you are more concerned by the Navigation so here is a sample code that worked for me to add a background image for iOS5 and iOS4. It works with transparent png.
Just add this in the loadView method of your viewController:
#define kBackgroundImageTag 42
UINavigationBar *theNavigationBar = self.navigationController.navigationBar;
UIImage *myBackgroundImage = [UIImage imageNamed:#"myImage.png"];
if([theNavigationBar respondsToSelector:#selector(setBackgroundImage:forBarMetrics:)]) //iOS5 stuff
{
[theNavigationBar setBackgroundImage:myBackgroundImage forBarMetrics: UIBarMetricsDefault];
}
else if(![theNavigationBar viewWithTag:kBackgroundImageTag]) //iOS 4 stuff
{
UIImageView *imageView = [[UIImageView alloc] initWithImage:myBackgroundImage];
imageView.tag = kBackgroundImageTag;
[theNavigationBar insertSubview:imageView atIndex:0];
}
Hope it helps !

colorWithPatternImage and setCornerRadius issue

Hello I'd like to achieve at the same time rounded corners and a background composed by tiling a little png (OPERATOR_VIEW_BACKGROUND_IMAGE). My main goal is to allow a designer to fill the background of a View by inserting the right image in the project resources.
[triggerView setFrame:CGRectMake(0, 0, ICONS_WIDTH, iconFrameHeight)];
[triggerView.layer setCornerRadius:borderRadius];
[triggerView setBackgroundColor:[UIColor colorWithPatternImage:[UIImage imageNamed:OPERATOR_VIEW_BACKGROUND_IMAGE]]];;
I don't know why but triggerView loose the CornerRadius setting when I add the last line.
triggerView is a UIView built with interface builder and modified in its superView viewDidLoad programmatically, with the code above.
Where I'm wrong?
EDIT: I haven't mentioned that If I use a simple UIColor like: [UIColor orangeColor] It works well. So It's something related to the patternImage thing.
EDIT: I've tried also this code, working on the layer background of my view:
[triggerView setFrame:CGRectMake(0, 0, ICONS_WIDTH, iconFrameHeight)];
triggerView.backgroundColor = [UIColor clearColor];
UIImage *img = [UIImage imageNamed:OPERATOR_VIEW_BACKGROUND_IMAGE];
triggerView.layer.backgroundColor = [UIColor colorWithPatternImage:img].CGColor;
triggerView.layer.cornerRadius = radius;
[img release];
[self.view addSubview:triggerView];
Now I get a transparent background but the corners are rounded;
OK, thanks to Ben's comment and this Blog entry I've found this solution:
[triggerView setFrame:CGRectMake(0, 0, ICONS_WIDTH, iconFrameHeight)];
triggerView.layer.masksToBounds = YES;
triggerView.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:OPERATOR_VIEW_BACKGROUND_IMAGE]];
triggerView.layer.cornerRadius = radius;
[self.view addSubview:triggerView];
Seems that triggerView.layer.masksToBounds = YES; was the missing piece, but I still don't understand why triggerView.layer.cornerRadius = radius; alone didn't suffice.
Try setting the content property of the layer with the CGImageRef of the image:
triggerView.layer.contents = (id) [[UIImage imageNamed:OPERATOR_VIEW_BACKGROUND_IMAGE] CGImage];
You may have to alloc or retain the UIImage to prevent it being autoreleased...
With iOS SDK 4.0 and 4.1, colorWithPatternImage method has got a bug that show badly an image...
I used this method and I had this bug but I could resolve using another method...
Try to use the initWithPatternImage method of UIColor class:
UIColor *imageBg = [[UIColor alloc] initWithPatternImage:[UIImage imageNamed:OPERATOR_VIEW_BACKGROUND_IMAGE]];
This worked greatly for me...
Unfortunately I never used cornerRadius method and I don't know a possibly solution for this other problem.

iPhone development: How to create colored or translucent background for UIActionSheet?

When you try deleting a note in iPhone's Notes application, an UIActionSheet pops up. The sheet is translucent (but not black translucent). How is that achieved? Is it possible to make the background of UIActionSheet a certain color?
I usually implement the following delegate method:
- (void)willPresentActionSheet:(UIActionSheet *)actionSheet
Just to make a sample. In this case I use a stretched png as a background:
- (void)willPresentActionSheet:(UIActionSheet *)actionSheet {
UIImage *theImage = [UIImage imageNamed:#"detail_menu_bg.png"];
theImage = [theImage stretchableImageWithLeftCapWidth:32 topCapHeight:32];
CGSize theSize = actionSheet.frame.size;
// draw the background image and replace layer content
UIGraphicsBeginImageContext(theSize);
[theImage drawInRect:CGRectMake(0, 0, theSize.width, theSize.height)];
theImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
[[actionSheet layer] setContents:(id)theImage.CGImage];
}
and this is the result:
alt text http://grab.by/4yF1
You can use the code below:
actionSheetObj.actionSheetStyle=UIActionSheetStyleBlackOpaque;
or
actionSheetObj.actionSheetStyle=UIActionSheetStyleBlackTranslucent;
actionSheetObj.actionSheetStyle=UIActionSheetStyleBlackTranslucent;
It's not too difficult. You can use the following code:
CGSize mySize = myActionSheet.bounds.size;
CGRect myRect = CGRectMake(0, 0, mySize.width, mySize.height);
UIImageView *redView = [[[UIImageView alloc] initWithFrame:myRect] autorelease];
[redView setBackgroundColor:[UIColor colorWithRed:1.0 green:0.0 blue:0.0 alpha:0.5]];
[myActionSheet insertSubview:redView atIndex:0];
Just make sure you present the UIActionSheet before doing this or the size wont be set. That makes it kind of ugly, but you could do something like:
[myActionSheet showInView:self.view];
if (!redAdded) {
redAdded = YES;
//THE ABOVE CODE GOES HERE
}
You can definitely adjust the opacity by setting the alpha value. Interface Builder lets you edit the value directly, but in code I think you would do:
[[myActionSheet view] setOpaque:NO];
[[myActionSheet view] setAlpha:0.5];
I'm not sure if you need the setOpaque call or not - I think that is used to help optimize performance, as the iPhone won't try to render anything hidden by the opaque view.
It looks black to me (note: using 2.2.1). The only reason there's a color to it is because of the yellow behind it.
One option would be to use the black transparent background and find out the size and speed of the action sheet. Then create a view and animate it in at the same time you show the action sheet, just underneath it, to give it a tint different than the color naturally behind the action sheet. You would have to make the color view also translucent so you could see behind that as well.
I'm not sure if you can, but you might also try adjusting the opacity of the action sheet itself as well.