Changing the size of the UIImage - objective-c

I have the following code
else if([annotation.title isEqualToString:NSLocalizedString(#"TITLE 1",nil)]|| [annotation.title isEqualToString:NSLocalizedString(#"TITLE 2",nil)])
{
static NSString *identifier = #"currentLocation";
SVPulsingAnnotationView *pulsingView = (SVPulsingAnnotationView *)[self.mapView dequeueReusableAnnotationViewWithIdentifier:identifier];
if(pulsingView == nil) {
pulsingView = [[SVPulsingAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:identifier];
pulsingView.annotationColor = [UIColor colorWithRed:0 green:0.678431 blue:0 alpha:1];
pulsingView.image = [UIImage imageNamed:#"image.png"];
pulsingView.frame = CGRectMake(0, 0, 15, 15);
pulsingView.canShowCallout = YES;
}
return pulsingView;
Basically it's an image and besides it we have a pulsing round annotation.
When the image is tapped - the annotation description with either "Title 1" or "Title 2" comes up depending on the conditions (which are not relevant here).
The issue is simple - when i apply
pulsingView.frame = CGRectMake(0, 0, 15, 15);
The image on top of the pulsing view doesn't change size.
Need help with alternative solutions.
Thank you so much guys!

1) Try this
[pulsingView setFrame: CGRectMake(0, 0, 15, 15)];
instead of
pulsingView.frame = CGRectMake(0, 0, 15, 15);
2) If the first solution doesn't work maybe you need to disable the autolayout if you are using Storyboard or...
3) if you use XIB maybe you need to add
[pulsingView setNeedsDisplay];

If previous answer doesn't work, a little trick that I've used sometimes is calling setTranslatesAutoresizingMaskIntoConstraints:.
Your code would look like:
[pulsingView setTranslatesAutoresizingMaskIntoConstraints:YES];

Related

Only last image is loading from url in scrollview

In my iOS app I have have a lots of view and image and labels are the subviews of the. view. My problem is while loading images from url or remote server only last image is loading from url . Other images are loaded with placeholders. Tried many ways but unable to solve it . My code and screenshot are provided below
catScrollView=[[UIScrollView alloc]initWithFrame:CGRectMake(0, 10, self.view.frame.size.width,300)];
catScrollView.contentSize = CGSizeMake(([popularCategoryArray count] * 90)/1.8 + 10, catScrollView.frame.size.height);
[_parentScrollView addSubview:catScrollView];
catScrollView.backgroundColor=UIColorFromRGB(0xE8F7FE);
catScrollView.userInteractionEnabled=YES;
catScrollView.scrollEnabled=YES;
catScrollView.delegate=self;
for(int i =0;i<[popularCategoryArray count];i++)
{
Category* category=[popularCategoryArray objectAtIndex:i];
UIView *catView = [[UIView alloc] init];
if(i>=[popularCategoryArray count]/2+1)
{
catView.frame = CGRectMake(10 + y * 100, 4*10+90+10, 90, 120);
y++;
}
else
{
catView.frame = CGRectMake(10 + x * 100, 10, 90, 120);
x=i;
}
imageCat=[[UIImageView alloc]initWithFrame:CGRectMake(10, 0, 70, 90)];
imageCat.backgroundColor=[UIColor clearColor];
[imageCat sd_setImageWithURL:[NSURL URLWithString:category.ImageURL]
placeholderImage:[UIImage imageNamed:#"wish_list"]];
[catView addSubview:imageCat];
UILabel *labelCat=[[UILabel alloc]initWithFrame:CGRectMake(0, 95, catView.frame.size.width, 25)];
labelCat.backgroundColor=[UIColor clearColor];
labelCat.text=category.BngTitle;
labelCat.textAlignment=NSTextAlignmentCenter;
labelCat.font=[UIFont fontWithName:#"HelveticaNeue-Thin" size:12.0];
[catView addSubview:labelCat];
catView.backgroundColor=[UIColor clearColor];
[catScrollView addSubview:catView];
}
Screenshot
What is happening is that in UIView+Webcache.m's sd_internalSetImageWithURL method, the following code is executed on every run through your loop.
NSString *validOperationKey = operationKey ?: NSStringFromClass([self class]);
[self sd_cancelImageLoadOperationWithKey:validOperationKey];
The operationKey is always the same because in your default invocation it is not set, so it's possed as nil, and when it's nill it's just the class name. SDWebImage has a very aggressive policy of cancelling all previous requests that way.
Some people believe it only does that for multiple requests of the same URL, but this code does not vary on URL so that may be a bug or not. Your best bet may be to call the sd_internalSetImageWithURL method and set your own operationKey (something with a number in it so that they're all different).

Can't set image as the background of UILabel

I have seen lots of questions in stackoverflow with the same issue, but none of the solutions helped me. I have used the following code to set a custom image as the background of a label:
labelBackgroundImageOriginal = [UIImage imageNamed:#"paper.jpg"];
labelBackgroundImageSize = myLabel.frame.size;
UIGraphicsBeginImageContext(labelBackgroundImageSize);
[labelBackgroundImageOriginal drawInRect:CGRectMake(0, 0, labelBackgroundImageSize.width, labelBackgroundImageSize.height)];
labelBackgroundImageNew = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
myLabel.backgroundColor = [UIColor colorWithPatternImage:labelBackgroundImageNew];
But the label background was still white and the image didn't show up. Then I tried with the various solutions that were given under different questions like the following one by one:
myLabel.backgroundColor = [UIColor clearColor];
myLabel.opaque = NO;
But none of them seem to be working as well. Is there actually a way out of this?
Have you try using [UIColor colorWithPatternImage:] directly with your image ? It works fine for me.
UILabel *test = [[UILabel alloc] initWithFrame:CGRectMake(100, 100, 300, 100)];
test.text = #"TEST";
test.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:#"anyPicture"]];
[self.view addSubview:test];
EDIT : I've tried your code and it works fine ! Have you checked if myLabel.frame.size value is not null ?

UIScrollView buggy

Please excuse poor code atm, just trying to get it working before making it look nice. So I am trying to get UIScrollView working. If I remove all scroll view stuff, I have a nicely laid out page with 64 buttons, 32 on each side of the page. These buttons are nigh miniature; so I wanted to implement zoom to be able to click them.
Zoom currently has unexpected results. When the page starts, it is blank. Zooming unexpectedly shows some of the left side of the graph on the rigght side of the page, and it bounces as I try to scroll over towards it. But when I zoom more, it allows me to scroll more towards the middle of the buttons. Always giving me difficulties/bugging out as I scroll/zoom. So obviously unusable.
My viewDidLoad:
[super viewDidLoad];
UIScrollView *scroll = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];
scroll.minimumZoomScale = 0.5;
scroll.maximumZoomScale = 3.0;
scroll.delegate = self;
CGFloat yOrigin = self.view.frame.size.width;
UIView *awesomeView = [[UIView alloc] initWithFrame:CGRectMake(yOrigin, 0, self.view.frame.size.width, self.view.frame.size.height)];
// iterate over values in the staff array
int heightBetweenBrackets = 0;
int widthBetweenBrackets = 0;
int heightFromTop = 45;
for(int i = 0; i < 64; i++)
{
if(i == 32)
{
heightBetweenBrackets = 0;
}
UIButton *myButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
myButton.frame = CGRectMake(
0 + i/32*438,
heightFromTop + i%32*3+ heightBetweenBrackets,
35, 6);
[myButton setTitle:[NSString stringWithFormat:#"%d",i] forState:UIControlStateNormal];
myButton.titleLabel.adjustsFontSizeToFitWidth = YES;
[myButton.titleLabel setFont:[UIFont fontWithName:#"Arial" size:7]];
myButton.contentHorizontalAlignment = UIControlContentHorizontalAlignmentCenter;
// [myButton addTarget:self action:#selector(chooseWinner:) forControlEvents:UIControlEventTouchUpInside];
[awesomeView addSubview:myButton];
heightBetweenBrackets += (i%2 -1 * -1) * 3;
}
[scroll addSubview:awesomeView];
scroll.contentSize = CGSizeMake(self.view.frame.size.width, self.view.frame.size.height);
[self.view addSubview:scroll];
and:
- (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView
{
return self.view;
}
I apologize if the bug is stupid I'm just learning IOS =] thanks for your time
EDIT: Figured it out.
For anybody in the future: I ported over a pagination scroller, and didn't realize I had kept CGFloat yOrigin = self.view.frame.size.width; -- this of course was starting the view directly to the right of any visible space. Thus I was able to zoom and see the left of it, in a buggy manner, although it started out blank. Simply changing this to 0 solved my problem.
For anybody in the future: I ported over a pagination scroller, and didn't realize I had kept CGFloat yOrigin = self.view.frame.size.width; -- this of course was starting the view directly to the right of any visible space. Thus I was able to zoom and see the left of it, in a buggy manner, although it started out blank. Simply changing this to 0 solved my problem.

Positioning UIViews within UIScrollViews

I have added 9 UIViews as subviews of UIScrollView with the below code:
-(void)constructedViewsForArray:(NSArray*)_listOfItems {
[_scrollView setContentSize:CGSizeMake(_listOfItems.count * 160, _scrollView.frame.size.height)];
CGRect frame = CGRectMake(10, 5, 100, 90);
for (int i = 0; i<[_listOfItems count];i++) {
UIView *aView = [[UIView alloc]init];
UILabel *test = [[UILabel alloc]initWithFrame:CGRectMake(0, 20, 100, 20)];
test.text = [NSString stringWithFormat:#"%#",[_listOfItems objectAtIndex:i]];
[aView addSubview:test];
[aView setBackgroundColor:[UIColor redColor]];
frame.origin.x=((frame.size.width+5)*i+5);
aView.frame = frame;
[_scrollView addSubview:aView];
}
}
The view's are added correctly to the scrollview.But after scrolling the UIView's are not aligned. Please refer to the images below:
The 5px space between the first view and the screen edge is missing after scrolling.
How can I set the view positions after scrolling.
PS: Paging is enabled.
Thanks for any help.
Regards,
iSee
I think what you are missing is that there need to be 10 px between 3 and 4, and 6 and 7, etc. for paging to work like you want it.
So changing the line
frame.origin.x=((frame.size.width+5)*i+5);
to something like this
frame.origin.x=((frame.size.width+5)*i+5 + (i/3) * 5);
It looks you are not noticing that the 4th column requires 10 px margin.so you need to add 5 additional pixel after 3rd column.
if((i%3)==0)
frame.origin.x=(((frame.size.width+5)*i+5)+ (i*5));
else
frame.origin.x=((frame.size.width+5)*i+5);
I hope this will help.

UIImageView blurs image

I have a really weird problem with UIImageView. I have an image (an RGB png) 45x45 pixels which I add to the view. I can see that image is blurred after added to the view. Here is the same image in the simulator (left) and in Xcode (right):
(source: partywithvika.com)
(source: partywithvika.com)
I have custom UIImageView class with this initWithImage code:
- (id) initWithImage:(UIImage*) image {
self = [super initWithImage:image];
self.frame = CGRectMake(0, 0, 45, 45);
self.contentMode = UIViewContentModeScaleAspectFit;
self.quantity = 1;
if (self) {
self.label = [[UITextField alloc]initWithFrame:CGRectMake(0,40,45,25)];
self.label.font = [UIFont systemFontOfSize:16];
self.label.borderStyle = UITextBorderStyleNone;
self.label.enabled = TRUE;
self.label.userInteractionEnabled = TRUE;
self.label.delegate = self;
self.label.keyboardType = UIKeyboardTypeNumbersAndPunctuation;
self.label.textAlignment = UITextAlignmentCenter;
}
self.userInteractionEnabled = TRUE;
// Prepare 3 buttons: count up, count down, and delete
self.deleteButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
self.deleteButton.hidden = NO;
self.deleteButton.userInteractionEnabled = YES;
self.deleteButton.titleLabel.font = [UIFont systemFontOfSize:20];
self.deleteButton.titleLabel.textColor = [UIColor redColor];
[self.deleteButton setTitle:#"X" forState:UIControlStateNormal];
[self.deleteButton addTarget:self action:#selector(deleteIcon:) forControlEvents:UIControlEventTouchUpInside];
self.upCountButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
self.upCountButton.hidden = NO;
self.upCountButton.userInteractionEnabled = YES;
[self.upCountButton setTitle:#"+" forState:UIControlStateNormal];
[self.upCountButton addTarget:self action:#selector(addQuantity:) forControlEvents:UIControlEventTouchUpInside];
self.downCountButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
self.downCountButton.hidden = YES;
self.downCountButton.userInteractionEnabled = YES;
[self.downCountButton setTitle:#"-" forState:UIControlStateNormal];
[self.downCountButton addTarget:self action:#selector(removeQuantity:) forControlEvents:UIControlEventTouchUpInside];
return self;
}
I create it like this:
UIImage *desertIcon = [UIImage imageNamed:#"desert.png"];
IconObj *desertIconView = [[IconObj alloc] initWithImage:desertIcon];
desertIconView.center = CGPointMake(265,VERTICAL_POINT_ICON);
desertIconView.type = [IconObj TYPE_DESERT];
[self.view addSubview:desertIconView];
[desertIconView release];
Why would the displayed image be so than the one stored in a file?
In general, everything you do on iPhone with UIKit should be pixel-aligned. Problems with pixel alignment usually manifest as blurriness (this is especially true with text and images). This is why when I find blurry view, I first check if I'm setting the center property. When you set center, the frame's x, y, height and width are adjusted around that center point... frequently resulting in fractional values.
You could try using CGRectIntegral on the frame as shown:
desertIconView.center = CGPointMake(265,VERTICAL_POINT_ICON);
desertIconView.frame = CGRectIntegral(desertIconView.frame);
This may work, but if it doesn't, try setting the frame manually, without using center property to ensure that no values are fractional.
Edit: Whoops! Didn't notice that the OP had answered his own question... I'll leave this up for informational reasons anyway.
I had this problem and it was driving me nuts. After some investigation it turned out that my image was smaller than the frame, and hence the scaling up blurred it. Once I had put in higher resolution images the problem is gone.
Make sure your image size is greater than your UIImageView frame size.
Your IconObj is 45 pixels wide. You move your IconObj center to 265 which makes its frame to (242.5, VERTICAL_POINT_ICON - 25*0.5, 45, 25). Image will always be blur if some of frame parameter is not integer.
Solution, calculate the frame parameter yourself (don't use center). And always make it integer (cast to NSInteger, use ceil, floor, or round).
desertIconView.frame = CGRectMake((NSInteger)(265 - 45*0.5),
(NSInteger)(VERTICAL_POINT_ICON - 25*0.5),
45, 25);
What I ended up doing is loading bigger picture into 45x45 UIImageView, of course with
contentMode = UIViewContentModeScaleAspectFit;
I just had the same problem. First I thought wtf do I need glasses?
Then I realized it´s just not possible to center an image (or label) when you give it an odd number. You need to resize your image to e.g. 46*46 if you want to center it and stay sharp.
Or you can leave it at 45*45 but then you need to decide whether you want your image to be mal-centered 1 pixel to the right, or 1 pixel to the left. (or leave it blurry).
Try to align your IconObj view at screen pixels. If it's really 45x45 then you should set the center to something like CGPointMake(x + 0.5f, y + 0.5f).
You should also double check image size in pixels (e.g in Preview.app Command-I).
complete source for the offset problem:
contentImage = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 0, 0)];
UIImage *cImage = [UIImage imageNamed:image];
self.contentImage.image = cImage;
[self.contentImage setFrame:CGRectMake(0, 0, cImage.size.width, cImage.size.height)];
CGFloat offset = 0.0f;
if ( ( (int)cImage.size.width % 2 ) && ( (int)cImage.size.height % 2 ) ) {
offset = 0.5f;
}
[self.contentImage setCenter:CGPointMake(256.0f + offset, 256.0f + offset)];