I am displaying UIImageViews programmatically. It works fine with the first UIView but when I try to display it on the smaller view, it does not resize.
Can you help me. Thanks in advance
By the way, I have a custom class of my UIView called courtView. I use this class for the two UIView
UIImageView *newImage;
NSSet *ballPosition = [self.startPosition valueForKey:#"ballPosition"];
for (BallPosition *courtPosition in ballPosition) {
PlayerPosition *player = [courtPosition valueForKey:#"playerPosition"];
if ([player.team isEqual: #(1)]) {
newImage = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"Btn_blue.png"]];
}
else if ([player.team isEqual: #(2)]){
newImage = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"Btn_red.png"]];
}
else if ([player.team isEqual: #(0)]){
newImage = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"Ball.png"]];
}
newImage.frame = CGRectFromString([courtPosition valueForKey:#"courtPosition"]);
[self.courtView addSubview:newImage];
}
try this code
UIImage *myImage = [UIImage imageNamed :#"myImage.extension"];
//if ur images are static, if your images are from an array then loop it out write the below inside the loop, make sure myImageView is a class,static component.
CGFloat height = myImage.size.height;
CGFloat width = myImage.size.width;
myImageView.frame = CGRectMake (myImageView.frame.origin.x,myImageView.frame.origin.y,height,width);
myImageView.image = myImage;
Hope this helps
Related
I was using PSButtonCell's for links but three individual ones took up too much space so I'm trying to create a custom cell with three buttons in one row; basically like 3 floating icons.
I've got some code for creating a tableview with icons, but currently I do not know how to space them properly (all the icons currently overlap), and I have no idea how I should go about adding tap listeners to the views. Does this look like something I could modify to do what I want? If not does anyone have a better solution they could provide for me? thanks a lot heres my code for the icons
- (id)tableView:(id)tableView viewForHeaderInSection:(NSInteger)section {
if (section == 1) {
UIView *headerView = [[UIView alloc] initWithFrame:(CGRect){{0, 0}, {320, kHeaderHeight}}];
headerView.backgroundColor = UIColor.clearColor;
headerView.clipsToBounds = YES;
// icon
UIImage *bugicon = [UIImage imageWithContentsOfFile:[NSString stringWithFormat:#"%#/Bug.png", kSelfBundlePath]];
UIImageView *bugiconView = [[UIImageView alloc] initWithImage:bugicon];
bugiconView.frame = (CGRect){{0, 21}, bugiconView.frame.size};
// bugiconView.center = (CGPoint){headerView.center.x, bugiconView.center.y};
bugiconView.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin|UIViewAutoresizingFlexibleRightMargin;
[headerView addSubview:bugiconView];
UIImage *payicon = [UIImage imageWithContentsOfFile:[NSString stringWithFormat:#"%#/Paypal.png", kSelfBundlePath]];
UIImageView *payiconView = [[UIImageView alloc] initWithImage:payicon];
payiconView.frame = (CGRect){{0, 21}, payiconView.frame.size};
// payiconView.center = (CGPoint){headerView.center.x, payiconView.center.y};
payiconView.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin|UIViewAutoresizingFlexibleRightMargin;
[headerView addSubview:payiconView];
UIImage *btcicon = [UIImage imageWithContentsOfFile:[NSString stringWithFormat:#"%#/bitcoin.png", kSelfBundlePath]];
UIImageView *btciconView = [[UIImageView alloc] initWithImage:btcicon];
btciconView.frame = (CGRect){{0, 21}, btciconView.frame.size};
// btciconView.center = (CGPoint){headerView.center.x, btciconView.center.y};
btciconView.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin|UIViewAutoresizingFlexibleRightMargin;
[headerView addSubview:btciconView];
You're adding all the subview is in same position. All UIView frame start from same 'x' position '0'. You need to change 2nd and 3rd frame x positon . ( Change your CGRect 'X' position) for second and third UIView.
bugiconView.frame = (CGRect){{0, 21}, bugiconView.frame.size};
payiconView.frame = (CGRect){{0, 21}, payiconView.frame.size};
btciconView.frame = (CGRect){{0, 21}, btciconView.frame.size};
Hope it helps...
I'm trying to set up my app so that on an iPhone4s, a different background image is displayed, but the code below produces a black background. Can anyone see what I'm doing wrong?
Thank you
UIImageView *bgImageView = [[UIImageView alloc] initWithFrame: self.view.bounds];
if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad){
bgImageView.image = [UIImage imageNamed: #"background_rest-ipad.png"];
}
else if ( [GUIHelper isPhone5] ) {
bgImageView.image = [UIImage imageNamed: #"info-screen-bg#2x.png"];
}
else {
bgImageView.image = [UIImage imageNamed: #"info-screen-bg-i4s.png"];
}
bgImageView.userInteractionEnabled = YES;
bgImageView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
[self.view addSubview: bgImageView];
You can use Asset Catalog and it will manage your images based on device...
Thought, with your code, make sure the names of the image files are correct and the files itself are present.
I'm trying to create some composite UIImage objects with this code:
someImageView.image = [ImageMaker coolImage];
ImageMaker:
- (UIImage*)coolImage {
UIView *composite = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 400, 400)];
UIImageView *imgView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"coolImage"]]; //This is a valid image - can be viewed when debugger stops here
[composite addSubview:imgView];
UIView *snapshotView = [composite snapshotViewAfterScreenUpdates:YES];
//at this point snapshotView is just a blank image
UIImage *img = [self imageFromView:snapshotView];
return img;
}
- (UIImage *)imageFromView:(UIView *)view
{
UIGraphicsBeginImageContextWithOptions(view.bounds.size, YES, 0.0);
[view drawViewHierarchyInRect:view.bounds afterScreenUpdates:NO];
UIImage * img = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return img;
}
I just get back a blank black image. How can I fix?
Supplying YES for -snapshotViewAfterScreenUpdates: means it needs a trip back to the runloop to actually draw the image. If you supply NO, it will try immediately, but if your view is off screen or otherwise hasn't yet drawn to the screen, the snapshot will be empty.
To reliably get an image:
- (void)withCoolImage:(void (^)(UIImage *))block {
UIView *composite = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 400, 400)];
UIImageView *imgView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"coolImage"]]; //This is a valid image - can be viewed when debugger stops here
[composite addSubview:imgView];
UIView *snapshotView = [composite snapshotViewAfterScreenUpdates:YES];
// give it a chance to update the screen…
dispatch_async(dispatch_get_main_queue(), ^
{
// … and now it'll be a valid snapshot in here
if(block)
{
block([self imageFromView:snapshotView]);
}
});
}
You would use it like this:
[someObject withCoolImage:^(UIImage *image){
[self doSomethingWithImage:image];
}];
The snapshotted view has to be drawn to the screen for a snapshot view to not be blank. In your case, the composite view must have a superview for drawing to work.
However, you should not be using the snapshotting API for this kind of action. It is very inefficient to create a view hierarchy for the sole purpose of creating an image. Instead, use the Core Graphics API's to setup a bitmap image context, perform drawing and get back the result using UIGraphicsGetImageFromCurrentImageContext().
The reason it's only rendering a black rectangle is because you're drawing the view hierarchy of the snapshot view, which is non-existent.
To make it work, you should pass composite as the parameter like so:
- (UIImage*)coolImage {
UIView *composite = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 400, 400)];
UIImageView *imgView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"coolImage"]]
[composite addSubview:imgView];
UIImage *img = [self imageFromView:composite];
// Uncomment if you don't want composite to have imgView as its subview
// [imgView removeFromSuperview];
return img;
}
I'm running into a problem trying to perform a certain effect on iOS with tableviews...I know this can be done with normal UIViewControllers, but can't seem to pull it off with UITableViewControllers.
The effect is showing a clear background image behind a tableview and and as the user scrolls down the tableview, the background blurs. No problem getting that to happen.
The issue I'm having is that using a UITableViewController, I can only seem to have 1 ImageView behind the TableView and to create the blurring effect, the extension i am using will require 2 image views, one of which is the blurred image and has it's opacity set to 0 at launch.
This is code I know works using a UIViewController:
UIImage *background = [UIImage imageNamed:#"bg_004.png"];
self.backgroundImageView = [[UIImageView alloc] initWithImage:background];
self.backgroundImageView.contentMode = UIViewContentModeScaleAspectFill;
[self.view addSubview:self.backgroundImageView];
self.blurredImageView = [[UIImageView alloc] init];
self.blurredImageView.contentMode = UIViewContentModeScaleAspectFill;
self.blurredImageView.alpha = 0;
[self.blurredImageView setImageToBlur:background blurRadius:10 completionBlock:nil];
[self.view addSubview:self.blurredImageView];
This is the code I am trying on the UITableViewController that does not work:
UIImage *background = [UIImage imageNamed:#"bg_004.png"];
self.backgroundImageView = [[UIImageView alloc] initWithImage:background];
self.backgroundImageView.contentMode = UIViewContentModeScaleAspectFill;
[self.tableView setBackgroundView:self.backgroundImageView];
// Also tried this, to no success
[self.tableView.backgroundView addSubview:self.blurredBackgroundView];
self.blurredBackgroundView = [[UIImageView alloc] init];
self.blurredBackgroundView.contentMode = UIViewContentModeScaleAspectFill;
self.blurredBackgroundView.alpha = 0;
[self.blurredBackgroundView setImageToBlur:background blurRadius:10 completionBlock:nil];
[self.tableView setBackgroundView:self.blurredBackgroundView];
// Also tried this, to no success
[self.tableView.backgroundView addSubview:self.blurredBackgroundView];
If anyone has some suggestions how how to make this work, i'd love some advice. I really wouldn't like to change my code from a TableViewController to a UIViewController
Set the table view's backgroundView property to point to an instance of UIView, and then add your image views as subviews of the new view. You can't directly add subviews to instances of UIImageView
I created a custom class called Slot and it is a subclass of UIView. In the slot class, the following function is called:
- (NSString*)assignItem:(Item*)item {
NSString *message;
if (item.slotSize + currentCapacity > slotSize) {
message = #"Sorry this item will not fit.";
} else {
//uiimageview stuff
UIImage *image = [[UIImage alloc] initWithContentsOfFile:item.imageName];
UIImageView *iv = [[UIImageView alloc] initWithImage:image];
[self addSubview:iv];
[items addObject:item];
currentCapacity = currentCapacity + item.slotSize;
message = #"Success";
}
NSLog(#"%#",message);
return message;
}
What happens is an Item (another custom class, subclass of NSObject) is passed to the Slot a UIImageView is created from the item from a string to an image in the bundle and it is added to the subview. However, the image isn't showing. The Success message is showing so I know its getting in there. Is there another way to add a subview from the subclass or am I just doing it all wrong?
UIImage *image = [[UIImage alloc] initWithContentsOfFile:item.imageName];
This is not the correct way to instantiate an image that you have in the bundle. You can either use imageNamed: like this,
UIImage *image = [UIImage imageNamed:item.imageName];
Or, you can get the image with initWithContentsOfFile like this,
NSString *imagePath = [[NSBundle mainBundle] pathForResource:item.imageName ofType:#"JPG"]; // replace JPG with whatever is appropriate for your image.
UIImage *image = [UIImage imageWithContentsOfFile:imagePath];
imageNamed: will cache the images, the other way doesn't.