I'm a bit lost here. I have a class subclassed from UIView called BalloonGuy. I initialize it like this.
- (id)initWithFrame:(CGRect)frame {
UIImage* loadedImage = [UIImage imageNamed:#"balloonguy.png"];
CGRect rect = CGRectMake(0, 0, loadedImage.size.width, loadedImage.size.height);
self = [super initWithFrame:rect];
image=[loadedImage retain];
self.opaque = YES;
self.backgroundColor = [UIColor clearColor];
return self;
}
I add it as a subview and everything is great.
What I can't do, however is change the image afterwards, ever. I won't list my failed attempts but what do I do?
I think you'll need to redraw your view when you change your image property, so overriding the property setter in this case may be a good idea:
- (void) setImage:(UIImage *) im {
[image release];
image = nil;
image = [im retain];
[self setNeedsLayout];
}
Related
I'm trying to display a UIImage selected from a UIImagePickerController inside a subclass of CALayer and add it to my superview but I can't get it to work.
This is how I Get the Image from the UIImagePickerController, which seems to work good :
- (IBAction)importImage:(id)sender
{
UIImagePickerController *imagePickerController = [[UIImagePickerController alloc] init];
imagePickerController.delegate = self;
imagePickerController.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
UIPopoverController *popover = [[UIPopoverController alloc] initWithContentViewController:imagePickerController];
[popover setPopoverContentSize:CGSizeMake(320,320)];
[popover presentPopoverFromRect:importBackgroundButton.frame inView:self.view permittedArrowDirections:UIPopoverArrowDirectionAny animated:YES];
self.popoverController = popover;
[imagePickerController release];
}
-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
[self.popoverController dismissPopoverAnimated:YES];
UIImage* img = [info objectForKey:UIImagePickerControllerOriginalImage];
[[context currentDrawing] setBkgImage:img];
}
This is my subclass of CALayer which contains the UIImage to display :
#interface UI_BackgroundImageLayer : CALayer
{
//This is for the Import image as background feature
UIImage* _bkgImg;
}
#property (nonatomic, retain) UIImage* _bkgImg;
-(void)update:(UIImage*)src;
#end
////
#implementation UI_BackgroundImageLayer
- (id)init
{
self = [ super init ];
// [self resetTransform ];
_bkgImg = nil;
return self;
}
-(void)update:(UIImage*)src
{
_bkgImg = [[UIImage alloc] initWithCGImage:src.CGImage];
self.contentsGravity = kCAGravityResizeAspect;
self.contents = (id)_bkgImg.CGImage;
}
#end
I update the content of the CALayer when the user select another image !
The CALayer is added to the superview from start like this :
[[_superView layer] insertSublayer:context._imgLayer atIndex:0];
It's inserted at index 0 because it's meant to be a background image so it must be under other UI elements and drawings.
All of this seems good to me, but nothing appears on the screen ...
Are you using XCode v4.2? If so, this may be relevant to you or it may not but it's worth a look. I've had exacty the same problem as you with CALayer subclasses not responding to the usual display methods (like setCornerRadius, setBackgroundColor etc...). Try this; in your hosting view, just after you've instanced the VIEW put the following line;
[self setBackgroundColor [UIColor clearColor]];
You may well find that from that point on, your layer subclass works perfectly.
My view subclass code looks like this;
// layerClass
+ (Class) layerClass {
return [myLayerSubclass class];
}
// View subclass init routine
- (id) initWithFrame: (CGRect) p_frame {
self = [super initWithFrame: p_frame];
if (self) {
[self setBackgroundColor: [UIColor clearColor]]; // *** BUG WORKARUND? *** Note: Do Apple know about this?
// Rest of your view init ....
}
return self;
}
It seems that the layer subclass does not respond until its hosting view has 'acknowledged' that it's there. It took me days to debug but now all my code works perfectly.
Hope this helps.
V.V.
I have a few UIToolbars throughout my app. I want each one to have a different background. Here is an example of a Category I have:
#import "UIToolbar+NavBarAdditions.h"
#implementation UIToolbar (Addition)
- (void) drawRect:(CGRect)rect {
UIImage *barImage = [UIImage imageNamed:#"rowbg.png"];
[barImage drawInRect:rect];
}
#end
I have a few images rowbg1, rowbg2 etc etc that I want to use for other UIToolbars that are inside of my app. How can I choose which category to use for the respective toolbar?
Change a toolbar background
Using a subclass
A category changes all UIToolbar classes. If you need different toolbars, let each client be in charge of setting the image:
#interface UIBgToolbar : UIToolbar {
#private
UIImage *_background;
}
#end
#implementation UIBgToolbar
- (id)initWithFrame:(CGRect)aRect imagen:(UIImage*)image {
if (self = [super initWithFrame:aRect]){
_background = [image retain];
}
return self;
}
- (void) drawRect:(CGRect)rect {
UIImage *barImage = _background;
[barImage drawInRect:rect];
}
-(void)dealloc {
[_background release];
[super dealloc];
}
#end
Usage:
UIImage *bg = [UIImage imageNamed:#"bar_bottom.png"];
// 416 = 480 - status bar (20) - navigation bar (44)
CGRect rect = CGRectMake(0, 416-bg.size.height, bg.size.width, bg.size.height);
UIBgToolbar *toolbar = [[UIBgToolbar alloc] initWithFrame:rect imagen:bg];
[self.view addSubview:toolbar];
[toolbar release];
Using a subview
This is an alternative way that adds a subview and doesn't need subclasses or categories:
UIImage *bg = [UIImage imageNamed:#"bar_bottom.png"];
// 416 = 480 - status bar (20) - navigation bar (44)
CGRect rect = CGRectMake(0, 416-bg.size.height, bg.size.width, bg.size.height);
UIToolbar *toolbar = [[UIToolbar alloc] initWithFrame:rect];
UIImageView *background = [[[UIImageView alloc] initWithImage:bg] autorelease];
background.frame = toolbar.bounds;
background.autoresizingMask = UIViewAutoresizingFlexibleWidth;
BOOL isIOS5 = [[[UIDevice currentDevice] systemVersion] intValue] >= 5;
toolbar insertSubview:background atIndex: (isIOS5 ? 1 : 0)];
[self.view addSubview:toolbar];
[toolbar release];
Making the background transparent.
Don't use this unless you really need a transparent background.
#interface UITransparentToolBar : UIToolbar
#end
#implementation UITransparentToolBar
- (void)drawRect:(CGRect)rect {
[[UIColor clearColor] set];
CGContextFillRect(UIGraphicsGetCurrentContext(), rect);
}
#end
Usage:
// create the toolbar
UIImage *bg = [UIImage imageNamed:#"bar_bottom.png"];
// 416 = 480 - status bar (20) - navigation bar (44)
CGRect rect = CGRectMake(0, 416-bg.size.height, bg.size.width, bg.size.height);
UIBgToolbar *toolbar = [[UIBgToolbar alloc] initWithFrame:rect];
// add the background
// self.backgroundColor = [UIColor clearColor];
UIImageView *background = [[UIImageView alloc] initWithImage:bg];
background.frame = toolbar.bounds;
background.autoresizingMask = UIViewAutoresizingFlexibleWidth;
BOOL isIOS5 = [[[UIDevice currentDevice] systemVersion] intValue] >= 5;
[toolbar insertSubview:background atIndex: (isIOS5 ? 1 : 0)];
[self.view addSubview:toolbar];
[toolbar release];
Change a navigation bar
Navigation bar background
Use Noah's code in a category.
Using a subclass is also possible:
Create a UINavigationToolbar subclass containing the drawRect method from Noah's answer. - Select your MainWindow.XIB, select "Navigation Bar", press ⌥⌘3 to (show Identity Inspector), and change the class to the class you just created.
Also while in IB, press ⌥⌘4 (show Attributes Inspector) and set a number in the field Tag. If you look at Noah's code, that number decides which image to use.
Navigation bar toolbar background
Same thing as for a normal toolbar but using a category because the navigation toolbar is read-only:
// UIToolbar.h
#interface UIToolbar (Transparency)
- (void)drawRect:(CGRect)rect;
#end
// UIToolbar.m
#import "TransparentToolbar.h"
#implementation UIToolbar (Transparency)
- (void)drawRect:(CGRect)rect {
[[UIColor clearColor] set];
CGContextFillRect(UIGraphicsGetCurrentContext(), rect);
}
#end
Usage:
// bar_bottom_bumped.png is a toolbar image with transparency
UIImage *bg = [UIImage imageNamed:#"bar_bottom_bumped.png"];
UIImageView *background = [[UIImageView alloc] initWithImage:bg];
background.frame = self.navigationController.toolbar.bounds;
background.autoresizingMask = UIViewAutoresizingFlexibleWidth;
BOOL isIOS5 = [[[UIDevice currentDevice] systemVersion] intValue] >= 5;
self.navigationController.toolbar.backgroundColor = [UIColor clearColor];
[self.navigationController.toolbar insertSubview:background atIndex: (isIOS5 ? 1 : 0)];
If you’re creating the toolbars yourself—in IB or through code—then Jano’s solution is mostly right, though I disagree with the idea of making it transparent: you should avoid doing unnecessary blending. If you need to replace the toolbars created by a navigation controller, though, you do need to use a category rather than subclassing. You can use your existing implementation; you just need to set a different tag on each of the toolbars you want to have a different appearance, then check that value in your -drawRect:. In other words, your view controller should have something like this:
- (void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
UIToolbar *toolbar = self.navigationController.toolbar;
toolbar.tag = 3; // different value for each controller, or however you want to choose the toolbar
[toolbar setNeedsDisplay];
}
and your toolbar category should have something like this:
- (void)drawRect:(CGRect)r
{
UIImage *barImage = nil;
switch(self.tag)
{
case 1:
barImage = [UIImage imageNamed:#"rowbg.png"];
break;
case 3:
barImage = [UIImage imageNamed:#"something else.png"];
break;
}
[barImage drawInRect:self.bounds];
}
You should use subclassing instead of categories.
I've got a UIImageView in a page that gets its image from the Interface builder and I'm trying to put small icons on the top of it (it's a small map and I want to put icons on it). I know it's probably very simple but I tried to look it up in the documentation but it pretty much got me more confused.
Using Interface Builder, can't you just drag a UIImageView into an existing UIImageView? In effect, you end up with one UIImageView embedded within another.
You should also be able to easily set the hidden property of the "small map" UIImageView in code, depending on if that UIImageView is needed or not.
Hope this helps. Good Luck.
Let It Be Known
you could compose your own UIView by adding both the large and small UIViewImage views.
I have illustrated my approach below with the Pseudocode .
-(id) initwithFrame:(CGRect) frame
{
if(self = [super initWithFrame:frame])
{
iContainer = [[UIView alloc] initWithFrame:frame];
[iContainer addSubViews:iLargerUIImageView];
[iContainer addSubViews:iSmallUIImageView];
[self.view addSubViews:iContainer];
}
return self;
}
-(void) layoutSubviews
{
CGRect myRect = self.frame;
iContainer.frame = myRect;
//Give the location to iLargerUIImageView as per your requirement.
iLargerUIImageView.frame = CGRectMake(...,...,...,...);
//Give the location to iSmallUIImageViewas per your requirement.
iSmallUIImageView.frame = CGRectMake(...,...,...,...);
}
-(void) dealloc
{
[iContainer release];
[iLargerUIImageView release];
[iSmallUIImageView release];
}
try this code:
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];
}
I'm trying to place various size images inside imageView of UITableViewCell. I get the image data asynch'ly, create the image, set the content mode of imageView and finally set bounds of imageView. But the code seems insensitive to any changes I made. I want the images to be centered in a 75x75 area. I wrote the below code for this purpose
UIImage* image = [UIImage imageWithData:data];
[holder.imageView setContentMode:UIViewContentModeCenter || UIViewContentModeRedraw];
[holder.imageView setImage:image];
[holder.imageView setBounds:CGRectMake(0,0,75,75)];
[holder.imageView setFrame:CGRectMake(0,0,75,75)];
[holder setNeedsLayout];
Where holder is the UITableViewCell. The result I get is always the same. All images have 75px height and different widths. Can someone help me solve this problem?
I have realized that setting contentMode and bounds properties does not have any effect in that code. I have added an NSLog after the last line and got the results as below:
NSLog(#"imageview:%# bounds and contentMode:%# %#",[holder imageView],[holder.imageView bounds],[holder.imageView contentMode]);
imageview:<UIImageView: 0x39ab8a0;
frame = (0 0; 75 75); opaque = NO;
userInteractionEnabled = NO; layer =
<CALayer: 0x39a92b0>> bounds and
contentMode:(null) (null)
Still no solution
Done, I finally found the solution, it cost me 3 hours though =)
The solution is to change properties like bound,frame,contentMode in -(void)layoutSubviews method of the custom UITableViewCell class. The "trick" is to write layout code in this method, otherwise the code does not have any effect.
Below code did the work for me. It makes rows of the table vertically aligned.
- (void)layoutSubviews {
[super layoutSubviews];
self.imageView.bounds = CGRectMake(0,0,75,75);
self.imageView.frame = CGRectMake(0,0,75,75);
self.imageView.contentMode = UIViewContentModeScaleAspectFit;
CGRect tmpFrame = self.textLabel.frame;
tmpFrame.origin.x = 77;
self.textLabel.frame = tmpFrame;
tmpFrame = self.detailTextLabel.frame;
tmpFrame.origin.x = 77;
self.detailTextLabel.frame = tmpFrame;
}
So the problem with UITableViewCell's is that you have no control over the size of the built-in objects (namely imageView, contentView, accessoryView, backgroundView). When the table changes, your customizations get trampled over.
You can, as Behlul pointed out, force the sizes to be correct by using layoutSubviews, but the problem with that is that layoutSubviews is called every time the table scrolls. That is a lot of unnecessary re-layout calls.
An alternate, method is to add all of your content to the contentView. Similarly if you are customizing the background, you can create a transparent backgroundView and add your custom background view (eg myBackgroundView) as a subview of backgroundView.
This way you can place and size your items how you want them.
The down side is the stock messages are no longer received from the accessory or image views. You just have to create you own.
Hope that helps!
// This code is not tested
// MyCustomTableViewCell
- (id) init{
self = [super initWithStyle: UITableViewCellStyleDefault reuseIdentifier:#"MyReuseIdentifier"];
if(self){
//image view
my_image_view = [[[UIImageView alloc] initWithImage:[UIImage imageNamed:#"default_image.png"]] retain];
[my_image_view setFrame:CGRectMake(10,10,30,30)];
[self.contentView addSubview:my_image_view];
//labels
my_text_label = [[[UILabel alloc] initWithFrame:CGRectMake(50,10,100,15)] retain];
[self.contentView addSubview:my_text_label];
//set font, etc
//detail label
my_detail_label = [[[UILabel alloc] initWithFrame:CGRectMake(50,25,100,15)] retain];
[self.contentView addSubview:my_detail_label];
//set font, etc
//accessory view
//Whatever you want to do here
//attach "accessoryButtonTapped" selector to button action
//background view
UIView* background_view = [[[UIView alloc] initWithFrame:CGRectMake(0, 0, 200, 50)] autorelease];
[background_view setBackgroundColor:[UIColor greenColor]];
background_view.layer.cornerRadius = 17;
background_view.layer.borderWidth = 3;
background_view.layer.borderColor = [UIColor whiteColor].CGColor;
[self setBackgroundView:[[[UIView alloc] init] autorelease]];
[self.backgroundView addSubview:background_view];
}
return self;
}
- (void) setLabelText: (NSString*) label_text{
[my_text_label setText:label_text];
}
- (void) setDetailText: (NSString*) detail_text{
[my_detail_label setText: detail_text];
}
- (void) accessoryButtonTapped{
//call table view delegate's accessoryButtonTappedForRowWithIndexPath method
}
"UIViewContentModeCenter || UIViewContentModeRedraw" is equivalent to 1. It's also not a bitfield. You want UIViewContentModeCenter.
UITableViewCell.imageView is managed by the cell. If you want custom layout, try adding a view to contentView (I'm guessing what you mean by "centered in a 75x75 area"):
UIImageView * iv = [[[UIImageView alloc] initWithImage:image] autorelease];
iv.frame = (CGRect){{0,0},{75,75}};
iv.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin| UIViewAutoresizingFlexibleRightMargin;
iv.contentMode = UIViewContentModeScaleAspectFit;
[holder.contentView addSubview:iv];
try changing the "contentMode" property of imageView to 'UIViewContentModeScaleAspectFit' or 'UIViewContentModeScaleAspectFill'
Create subclass of UITableViewCell:
#interface UITableViewCellSubClass : UITableViewCell
#end
#implementation UITableViewCellSubClass
- (void)layoutSubviews {
[super layoutSubviews];
self.imageView.frame = CGRectMake(0,4,32,32);
self.textLabel.frame = CGRectMake(42,4,300,32);
}
#end
I'm having a memory problem related to UIImageView. After adding this view to my UIScrollView, if I try to release the UIImageView the application crashes. According to the stack trace, something is calling [UIImageView stopAnimating] after [UIImageView dealloc] is called. However, if I don't release the view the memory is never freed up, and I've confirmed that there remains an extra retain call on the view after deallocating...which causes my total allocations to climb quickly and eventually crash the app after loading the view multiple times. I'm not sure what I'm doing wrong here though...I don't know what is trying to access the UIImageView after it has been released. I've included the relevant header and implementation code below (I'm using the Three20 framework, if that has anything to do with it...also, AppScrollView is just a UIScrollView that forwards the touchesEnded event to the next responder):
Header:
#interface PhotoHiResPreviewController : TTViewController <UIScrollViewDelegate> {
NSString* imageURL;
UIImage* hiResImage;
UIImageView* imageView;
UIView* mainView;
AppScrollView* mainScrollView;
}
#property (nonatomic, retain) NSString* imageURL;
#property (nonatomic, retain) NSString* imageShortURL;
#property (nonatomic, retain) UIImage* hiResImage;
#property (nonatomic, retain) UIImageView* imageView;
- (id)initWithImageURL:(NSString*)imageTTURL;
Implementation:
#implementation PhotoHiResPreviewController
#synthesize imageURL, hiResImage, imageView;
- (id)initWithImageURL:(NSString*)imageTTURL {
if (self = [super init]) {
hiResImage = nil;
NSString *documentsDirectory = [NSString stringWithString:[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject]];
[self setImageURL:[NSString stringWithFormat:#"%#/%#.jpg", documentsDirectory, imageTTURL]];
}
return self;
}
- (void)loadView {
[super loadView];
// Initialize the scroll view
hiResImage = [UIImage imageWithContentsOfFile:self.imageURL];
CGSize photoSize = [hiResImage size];
mainScrollView = [[AppScrollView alloc] initWithFrame:[UIScreen mainScreen].bounds];
mainScrollView.autoresizingMask = ( UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight);
mainScrollView.backgroundColor = [UIColor blackColor];
mainScrollView.contentSize = photoSize;
mainScrollView.contentMode = UIViewContentModeScaleAspectFit;
mainScrollView.delegate = self;
// Create the image view and add it to the scrollview.
UIImageView *tempImageView = [[UIImageView alloc] initWithFrame:CGRectMake(0.0, 0.0, photoSize.width, photoSize.height)];
tempImageView.contentMode = UIViewContentModeCenter;
[tempImageView setImage:hiResImage];
self.imageView = tempImageView;
[tempImageView release];
[mainScrollView addSubview:imageView];
// Configure zooming.
CGSize screenSize = [[UIScreen mainScreen] bounds].size;
CGFloat widthRatio = screenSize.width / photoSize.width;
CGFloat heightRatio = screenSize.height / photoSize.height;
CGFloat initialZoom = (widthRatio > heightRatio) ? heightRatio : widthRatio;
mainScrollView.maximumZoomScale = 3.0;
mainScrollView.minimumZoomScale = initialZoom;
mainScrollView.zoomScale = initialZoom;
mainScrollView.bouncesZoom = YES;
mainView = [[UIView alloc] initWithFrame:[UIScreen mainScreen].bounds];
mainView.autoresizingMask = ( UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight);
mainView.backgroundColor = [UIColor blackColor];
mainView.contentMode = UIViewContentModeScaleAspectFit;
[mainView addSubview:mainScrollView];
// Add to view
self.view = mainView;
[imageView release];
[mainScrollView release];
[mainView release];
}
- (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView {
return imageView;
}
- (void)dealloc {
mainScrollView.delegate = nil;
TT_RELEASE_SAFELY(imageURL);
TT_RELEASE_SAFELY(hiResImage);
[super dealloc];
}
I'm not sure how to get around this. If I remove the call to [imageView release] at the end of the loadView method everything works fine...but I have massive allocations that quickly climb to a breaking point. If I DO release it, however, there's that [UIImageView stopAnimating] call that crashes the application after the view is deallocated.
Thanks for any help! I've been banging my head against this one for days. :-P
Cheers,
Josiah
Move the release of imageView before the point where you set it. This way you release the object before changing where the imageView pointer points to:
// Create the image view and add it to the scrollview.
UIImageView *tempImageView = [[UIImageView alloc] initWithFrame:CGRectMake(0.0, 0.0, photoSize.width, photoSize.height)];
tempImageView.contentMode = UIViewContentModeCenter;
[tempImageView setImage:hiResImage];
// release old object
[imageView release];
// set the pointer to point at a new object
self.imageView = tempImageView;
[tempImageView release];
[mainScrollView addSubview:imageView];
Then at the bottom of your method where you release the other objects, remove the line:
[imageView release];
Ideally, class level variables should be released in the class' dealloc() method
Looks like imgView is a class level variable and you are releasing that at the end of the method call.
If you have a variable that you would like to release at the end of the method -- it should be a method-level variable. So, in your case, try making the imgView a method level variable. Then you should be able to release it at the end of the method without any problem
Why do you create a temporary view? Do you call loadView more than once?
self.imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0.0, 0.0, photoSize.width, photoSize.height)];
[self.imageView setContentMode:UIViewContentModeCenter];
[self.imageView setImage:hiResImage];
[mainScrollView addSubview:self.imageView];
[self.imageView release]
Or release it in dealloc since it's an instance variable? addSubview retains your UIImageView, so it would be weird if it crashed because the object isn't present. Have you tried setting out breakpoints?