Unable to load image into UIImage - objective-c

I have two images on Amazon S3 service. One loads fine and another does not. There isn't an error message. It just does not load.
[doodleImageView setImageWithURL:#"https://s3.amazonaws.com/doodlestash/doodles/20/thumb.jpg"];
//This one does not load.
[doodleImageView setImageWithURL:#"https://s3.amazonaws.com/doodlestash/doodles/20/original.jpg"];
Any thoughts?

The images do both appear to be "valid". You may want to double check the library you are using and check for typos. In a quick demo app I did the following:
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
UIImageView* image1 = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, 320, 200)];
image1.image = [UIImage imageWithData: [NSData dataWithContentsOfURL:[NSURL URLWithString:#"https://s3.amazonaws.com/doodlestash/doodles/20/original.jpg"]]];
UIImageView* image2 = [[UIImageView alloc]initWithFrame:CGRectMake(0, 200, 320, 200)];
image2.image = [UIImage imageWithData: [NSData dataWithContentsOfURL:[NSURL URLWithString:#"https://s3.amazonaws.com/doodlestash/doodles/20/thumb.jpg"]]];
[self.view addSubview:image1];
[self.view addSubview:image2];
}
I ended up with this:

Related

Showing only a portion of the original image in a UIImageView

How can i show only a portion of the original image in a UIImageView
This question may be very familiar and old, But the reason for asking again is,i could not find a workable idea with the help of those answers
Many said set image.contentMode = UIViewContentModeCenter; (but not working)
I need almost a rectangle containing the center of the original image,How do I get this ?
I do make this working,when i am displaying a static image to my app and setting Content mode of UIImageVie as Aspect Fill.
But this is not workable in the case when i am displaying an image from url and using NSData
Adding my code and the images
NSString *weburl = #"http://topnews.in/files/Sachin-Tendulkar_15.jpg";
UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(10, 50, 108, 86)];
NSURL *url = [NSURL URLWithString:weburl];
NSData *data = [NSData dataWithContentsOfURL:url];
imageView.image = [UIImage imageWithData:data];
[self.view addSubview:imageView];
If you added the UIImageView form XIB, you can find a "mode" property there and then you can see and set different modes from there (from Interface builder). Or by programatically, you can set different modes by
imageView.contentMode = UIViewContentModeCenter;
imageView.clipsToBounds = YES;
Try this:
self.imageView.layer.contentsRect = CGRectMake(0.25, 0.25, 0.5, 0.5);
self.imageView will display middle part of image. You can calculate itself the required values of CGRect ​
For this kind of output, you need to crop the image according to your requirement.
Cropping code as below which can be used.
-(UIImage *) CropImageFromTop:(UIImage *)image
{
CGImageRef imageRef = CGImageCreateWithImageInRect([image CGImage], CGRectMake(0, 12, image.size.width, image.size.height - 12));
UIImage *cropimage = [[[UIImage alloc] initWithCGImage:imageRef] autorelease];
CGImageRelease(imageRef);
return cropimage;
}
you try to scale image and then add image in UiImageView and set center that image then it is in center. code of scale image is
-(UIImage *)imageWithImage:(UIImage *)image scaledToSize:(CGSize)newSize{
UIGraphicsBeginImageContext(newSize);
[image drawInRect:CGRectMake(0, 0, newSize.width, newSize.height)];
UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return newImage;
}
then you set the center of that image and add in the image view i hope this is work

Xcode the image is way too large need to be the size of the png image used not massive

UIImageView *image = [[UIImageView alloc] initWithFrame:self.view.bounds];
image.animationImages = [NSArray arrayWithObjects:
[UIImage imageNamed:#"BluePin.png"],
[UIImage imageNamed:#"GreenPin.png"],
[UIImage imageNamed:#"RedPin.png"],
[UIImage imageNamed:#"YellowPin.png"],
nil];
image.animationRepeatCount = 0;
image.animationDuration = 1.0;
[image startAnimating];
[image setFrame: CGRectMake(point.x-(image.bounds.size.width/2), point.y- (image.bounds.size.width/2), image.bounds.size.width, image.bounds.size.height)];
[self.view addSubview: image];
[UIView animateWithDuration:2.0 delay:1.0 options:UIViewAnimationOptionCurveLinear animations:^{
[image setAlpha:0.0];
} completion:^(BOOL finished) {
[image removeFromSuperview];
}];
the images are coming out way too big they need to be their original size
basically creates the images animation where the user touches and it fades away after x seconds
You want the image view to be the same dimensions as the image it contains, right?
UIImage *image = [UIImage imageName:#"BluePin.png"];
UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0,0,image.size.width,image.size.height];
Note that I'm using a different variable naming scheme than yours to distinguish between an image and an image view.

images from an array

i create an nsmutableArray and i add for it an images. also i create an imageView that i want to show the images from the array, i tried to use animationImages but i nothing is happened, how can i fix it?
here is my code:
//Numbers images
UIImage *numberZero = [UIImage imageNamed:#"numberZero.png"];
UIImage *numberOne = [UIImage imageNamed:#"numberOne.png"];
UIImage *numberTwo = [UIImage imageNamed:#"numberTwo.png"];
UIImage *numberThree = [UIImage imageNamed:#"numberThree.png"];
UIImage *numberFour = [UIImage imageNamed:#"numberFour.png"];
UIImage *numberFive = [UIImage imageNamed:#"numberFive.png"];
UIImage *numberSix = [UIImage imageNamed:#"numberSix.png"];
UIImage *numberSeven = [UIImage imageNamed:#"numberSeven.png"];
UIImage *numberEight = [UIImage imageNamed:#"numberEight.png"];
UIImage *numberNine = [UIImage imageNamed:#"numberNine.png"];
//add numbers uiimage to numbersArray
numbersArray = [NSMutableArray arrayWithObjects:numberZero, numberOne, numberTwo, numberThree, numberFour, numberFive, numberSix, numberSeven, numberEight, numberNine, nil];
imgview1 = [[UIImageView alloc] init];
imgview1.animationImages = numbersArray;
imgview1.animationDuration = 2;
imgview1.animationRepeatCount=0;
[imgview1 startAnimating];
[imgview1 stopAnimating];
thanks!
You need to actually add imgview1 to something so that it displays. If imgview1 was created in Interface Builder then there is no need to assign. Also you stop your animation immediately after starting it.
//If created in IB comment out the next line
imgview1 = [[UIImageView alloc] init];
imgview1.animationImages = numbersArray;
imgview1.animationDuration = 2;
imgview1.animationRepeatCount=0;
//If not created in IB then add this to a view e.g:
[self.view addSubview:imgview1];
[imgview1 startAnimating];
//[imgview1 stopAnimating]; this is too soon to stop animating
I assume that by "nothing happened" you mean that you see the first image, but the sequence does not move beyond that.
This is probably because you stopAnimating too soon: it does not even get a chance to start!
i solve the problem, i guess the problem were because i didn't use init with frame to my imgview.
here is the final code:
UIImageView *imgview1 = [[UIImageView alloc] initWithFrame:CGRectMake(40, 90, 240, 240)];
imgview1.animationImages = numbersArray;
imgview1.animationDuration = 2;
imgview1.animationRepeatCount=1;
[imgview1 startAnimating];
[self.view addSubview:imgview1];
hope u understand...

Show an image from json

I'm not able to show an image that I load from a json file.
I'm parsing my json with JSONKit and everything works fine but I can't load an image in the UIImageview. I hope some of you can help me out there.
below my code I thought might be correct but it isn't.
UIImageView *image = [[UIImageView alloc] initWithFrame:CGRectMake(20, 20, 100, 115)];
image.image = [UIImage imageNamed:[detailView objectForKey:#"thumbnail"]];
[self.view addSubview:image];
[image release];
i think in json u are getting url of image. display image from url using below code
//NSURL *url = [NSURL URLWithString:#"http://192.168.1.2x0/pic/LC.jpg"];
NSURL *url = [NSURL URLWithString:[detailView objectForKey:#"thumbnail"]];
NSData *data = [NSData dataWithContentsOfURL:url];
UIImageView *subview = [[UIImageView alloc] initWithFrame:CGRectMake(0.0f, 0.0f,320.0f, 460.0f)];
[subview setImage:[UIImage imageWithData:data]];
[self.view addSubview:subview];
[subview release];
the code you posted
image.image = [UIImage imageNamed:[detailView objectForKey:#"thumbnail"]];
will try to find the image on your bundle named the string stored in [detailView objectForKey:#"thumbnail"]
As you mentioned, your images are from remote server, you have to download the image from your remote server.
UIImageView *image = [[UIImageView alloc] initWithFrame:CGRectMake(20, 20, 100, 115)];
image.image = [UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:[detailView objectForKey:#"thumbnail"]]]];
[self.view addSubview:image];
[image release]

changing uinavigationBar multiple background iOS 4.3

i used to use the code below to change the UINavigationBar Background :
the CODE:
- (void) drawRect:(CGRect)rect
{
UIImage *image = [UIImage imageNamed:#"NavBar.png"];
[image drawInRect:CGRectMake(0, 0, self.frame.size.width, self.frame.size.height)];
}
so now i need more than one background NavBar2.png ....
how to achieve this ?
Thanks
Try this code.
- (void) drawRect:(CGRect)rect
{
if(navigationBar.tag == 0){
UIImage *image = [UIImage imageNamed:#"NavBar.png"];
[image drawInRect:CGRectMake(0, 0, self.frame.size.width, self.frame.size.height)];
}
else if(navigationBar.tag == 1)
UIImage *image = [UIImage imageNamed:#"NavBar1.png"];
[image drawInRect:CGRectMake(0, 0, self.frame.size.width, self.frame.size.height)];
}
}
Tag logic mentioned by R.A. would definitely help you.
If you are developing an app on iOS5, then you can also take a look at :
- (void)setBackgroundImage:(UIImage *)backgroundImage forBarMetrics:(UIBarMetrics)barMetrics
I solve it by using my original code as image background then put titleView for the navigation bar
UIView *aView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 165, 44)];
UIImageView *imgv = [[UIImageView alloc] initWithFrame:aView.frame];
imgv.image = [UIImage imageNamed:#"NavBar01.png"];
[aView addSubview:imgv];
self.navigationController.navigationBar.topItem.titleView = aView;
[aView release];
[imgv release];