UIImageView image not retained during drawRect event in iOS7 - ios

I am transitioning an app project from iOS-6.1 and xcode4 to iOS-7 and xcode5. My original iOS-6.1 routine to draw on a transparent UIView above an UIImageView does not work in iOS7. Here is what I have done and how I have fixed it. I'm hoping someone can tell me how to do it better!
Using InterfaceBuilder, I create a square (320 x 320) UIImageView on my View Controller view. I then make a 320 x 320 UIView directly above it and use Interface Builder Attributes Inspector to make it transparent.
Then I link up the setters and getters in Interface Builder. The UIImageView #property is (nonatomic, retain). I then subclass the now-transparent UIView and do my drawing in drawRect.
In iOS6.1 this works just fine. In iOS-7 I have a problem.
(Several, actually). As a side note, in xcode5 I cannot get the Attributes Inspector to appear when I select the UIView, when I try to make the UIView transparent. Could this be a bug, perhaps in my installation of xcode5, or is the xcode5 Attributes Inspector no longer supporting UIViews?
So I make the UIView transparent programmatically in iOS-7. I'm doing it here in the UIView's drawRect, which is probably overkill, but I'm still trying to figure out what's going on:
self.backgroundColor = [UIColor clearColor];
self.opaque= NO;
self.window.backgroundColor = [UIColor clearColor];
and then I have to make the UIImageView appear, also in drawRect, using this code
UIImage *myImage = [UIImage imageNamed:#"myImage.png"];
CGRect imageRect;
imageRect.origin.x = self.bounds.origin.x + 10;
imageRect.origin.y = self.bounds.origin.y + 10;
imageRect.size.width = 300;
imageRect.size.height = 300;
[myImage drawInRect:imageRect];
And things are now fine. I get what I want which is my drawing on top of the image. But, it seems like a terrible performance hit to be calling myImage every time drawRect needs to be called.
Can anybody see why my original method of displaying the UIImageView once does not work in iOS-7? Has anybody a better solution to my expensive show-the-image-every-time method?
Thanks in advance
TFR

Related

(iOS) alpha not works when run app

I have hours trying to solve this and researching about this problem without results. This is my problem:
I'm using a ViewController in a existing project and over this I use an UIIMageView and a UIView at the same level. Then I set them alpha value in these two elements (alpha = 0.5) using Interface Builder. When I run the project on Simulator the alpha value not make effect and they looks like with their alpha value = 1. I made this same procedure in a new project and when I run the alpha effect is visible. I tried check/uncheck opaque option, set alpha value programatically as property or using method setAlpha, as well as set opaque value with code and it doesn't works. This problem happens with device too.
Anyone have a solution?
Check whether you import
#import <UIKit/UIKit.h>
UIIMageView.alpha = 0.4;
OR check with this
UIImage *wheelImage = [UIImage imageNamed:#"wheel#2x.png"];
UIImageView *imageView = [[UIImageView alloc]initWithImage:wheelImage];
imageView.frame = CGRectMake(50, 50, wheelImage.size.width, wheelImage.size.height);
imageView.alpha = 0.2;
[self.view addSubview:imageView];
Did you forget to connect your outlet in the storyboard and the property?

Upraded Xcode, I have 2 Identicle views with UIScrollView. One View adds components to UIScroll fine, the other does not

I have an app that has been working just fine for ages. I upgraded XCode and now 1 of my views is doing weird things.
None of the objects I attempt to add to the UIScrollview show up.
I have 2 screens that are basically identical in code (i literally copy and pasted). One of them works just fine, I click a button to move to the other screen, and it doesn't display ANY components I attempt to add to the scroll view.
Any ideas as to what could be wrong? Why it would suddenly stop adding components after upgrading Xcode?
I have a background image that I have tested and changed to self.view that changes just fine. But anything I try to add to my UIScrollView, nothing gets added. Some sample code...
my .h declaration
#interface OutgateInputViewController : UIViewController<UITextFieldDelegate,UIPickerViewDelegate>{
IBOutlet UIScrollView *outputsView;
a sample in my .m
UIGraphicsBeginImageContext(self.view.frame.size);
[[UIImage imageNamed:#"inputbackground.png"] drawInRect:self.view.bounds];
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
self.view.backgroundColor = [UIColor colorWithPatternImage:image];
int x = 20;
int y = 20;
CGRect myImageRect = CGRectMake(0, 15, 320, 32.5);
UIImageView *myImage = [[UIImageView alloc] initWithFrame:myImageRect];
NSString *imgName = #"screen_logo.png";
[myImage setImage:[UIImage imageNamed:imgName]];
[outputsView addSubview:myImage];
y = y + 30;
pretty standard stuff. its an EXACT copy up to this point of the screen that works. (even a copy long after, i just pull data from a different location). So any ideas on what changed?
I went back and re-did everything from scratch, and apparently for some reason THIS View needs the additional __weak at the start of the declaration in the .h ...
no idea why as of yet, but this solved the issue of the entire view being broken.
__weak IBOutlet UIScrollView *outputsView;

UIScrollView with pattern image as background

here is the problem I'm facing: I am making a UIScrollView which will have a pattern image to fill the background. That is to say, I need a background to scroll with the UIScrollView. A good example for this is the Game Center app, on the iPad. The background will scroll smoothly with scrollview.
Currently I have two ways to realize this effect, but neither of them offered good performance.
First I tried this
self.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:kRLCastViewBGUnit]];
but unfortunately, the scrolling performance was very bad and occupied too much memory.
Then I tried to use CGContextDrawTiledImage in the drawRect like this:
- (void)drawRect:(CGRect)rect
{
CGContextRef currentContext = UIGraphicsGetCurrentContext();
CGContextTranslateCTM(currentContext, 0, rect.size.height);
CGContextScaleCTM(currentContext, 1.0, -1.0);
CGContextClipToRect(currentContext, rect);
CGRect centerTileRect = CenterTileRect;
CGContextDrawTiledImage(currentContext, centerTileRect, [ResourceProvider backgroundTileImageRef]);
}
It's still not satisfactory on the new iPad. I must have done something wrong or misused some methods, because the Game Center performs just great when it scrolls. Anyone can offer me a solution to solve the performance issue for the UIScrollView background? Thanks a lot!!
You should use an UITableView instead. In this you can set cell.backgroundView easily.
UIImageView *backgroundView = [[UIImageView alloc] initWithFrame:CGRectMake:(0.0,0.0,320.0, height)];
backgroundView.image = [UIImage imageNamed:#"Shelf.jpeg"];
cell.backgroundView = backgroundView;
And If you really want to use only UIScrollView then take only one cell background image and image width should be same of scrollView's width because colorWithPatternImage: method work like tiled property.
Take Only this image into your project and use it as backgroundColor of UIScrollView.
[scrollView setContentSize:CGSizeMake(320.0, 1000.0)];
[scrollView setBackgroundColor:[UIColor colorWithPatternImage:[UIImage imageNamed:#"cellImage.png"]]];

Resize UILabel correct on top of UIImageView

My problem is as follows:
I have on top of a UIImageView some labels. The image is a formular and the UILabels represents the entries.
If I rotate the device the image inside UIImageView gets resized with option "aspect fit".
How can I achieve that the UILabel is also resized and aligned correct?
The normal options for resizing aren't working as needed.
The UIImageView is zoomable with minimumZoomScale and maximumZoomScale set.
Thanks.
You can resize a UILabel proportionally using:
label.transform = CGAffineTransformMakeScale(0.5, 0.5);
You can reposition a UILabel relatively using:
label.frame = CGRectOffset(label.frame, deltaX, deltaY);
The rest is maths =)
So I managed to solve your problem. Full source code here.
Basically I removed your UIImageView and replaced it with a UIView. Then I added your UILabels within the UIView and removed any autosizing from the UIView and UILabels in interface builder. That way when the UIView gets resized any content in it will get resized/repositioned as well.
The next step was to change the class of the UIView to UIImageView in interface builder. Now from the source code I could set an image to it using imgView.image = [UIImage imageNamed:#"imagename.png"]; as well as I could set the mode using imgView.contentMode = UIViewContentModeScaleAspectFit;.
The transformation at rotation now happens with:
if (UIDeviceOrientationIsLandscape(deviceOrientation)){
// Play with the scale if you have any other images with different ratio.
float scale = 2.3f/3.0f;
// Transforming the UIImageView containing the UILabels which actually is a simple UIView
imgView.transform = CGAffineTransformMakeScale(scale, scale);
} else {
// Original size again
imgView.transform = CGAffineTransformMakeScale(1, 1);
}

iPhone Custom UISlider preload and rounded edges issues

I'm trying to implement a custom UISlider, I've extended it with a class called UISliderCustom which has the following code:
#implementation UISliderCustom
- (id)initWithCoder:(NSCoder *)aDecoder{
if(self == [super initWithCoder:aDecoder]){
self.frame = CGRectMake(self.frame.origin.x, self.frame.origin.y, 200, 13);
UIImage *slideMin = [[UIImage imageNamed:#"slideMinimum.png"] resizableImageWithCapInsets:UIEdgeInsetsMake(0, 5, 0, 0)];
UIImage *slideMax = [[UIImage imageNamed:#"slideMaximum.png"] resizableImageWithCapInsets:UIEdgeInsetsMake(0, 5, 0, 0)];
[self setThumbImage:[UIImage imageNamed:#"slideThumb.png"] forState:UIControlStateNormal];
[self setThumbImage:[UIImage imageNamed:#"slideThumb.png"] forState:UIControlStateHighlighted];
[self setMinimumTrackImage:slideMin forState:UIControlStateNormal];
[self setMaximumTrackImage:slideMax forState:UIControlStateNormal];
}
return self;
}
#end
I ran into two small problems
When I slide over the slider to one of the edges (progress = 0.0 / progress = 1.0), I can clearly see "left overs" in the sides, im not sure how to handle that as well, unfortunately :)
Slider images:
Problem:
I see the regular UISlider (blue and silver) for a couple of seconds, and only then the custom graphics is loaded, or when i actually move the slider. I'm not sure why this is happening.. EDIT: This only happens in the simulator, works fine now.
Thanks in advance for any assistance :)
Shai.
You have no need to subclass UISlider to achieve this effect, and if you did you certainly wouldn't set the track images in the drawRect method. drawRect should contain drawing code only, it is called whenever any part of the control needs redrawing.
Set the thumb and track images in a separate method, either within your subclass (called from initWithFrame and initWithCoder) or in the object that creates the slider in the first place. This only needs to be done once, when the slider is first created. Don't override drawRect.
You don't need to call awakeFromNib manually either, unless you have some specific code in there as well? That would be a common place to set custom images in a subclass, if you only ever used the slider from IB.
For the square ends, the problem is that the extreme edge of your track image is square, so it is showing around the thumb. Make both ends of the track image rounded, with a 1px stretchable area in the middle, like this:
I just had a very similar problem myself. It turned out that the size (width x height) of the slider that I added in interface builder didn't match the sizes of the images I was using to customize the slider. Once I made them match, those "leftovers" at the ends of the slider went away.

Resources