While loading the images from local XML file like this ,
<?xml version="1.0" encoding="UTF-8"?>
<Books>
<book id="1">
<img>/Users/admin/imagefolder/Main_pic.png</img>
</book>
</Books>
Am parsing the file and it parse the image from file. I can see in NSLog output as
<UIImage: 0x71883d0>
but when i view it in iphone as UIImage view, no image is shown. i had set the image in viewDidLoad Method as like this,
- (void)viewDidLoad{
[super viewDidLoad];
app = [[UIApplication sharedApplication]delegate];
UIImage *image = [UIImage imageWithContentsOfFile:#"/Users/admin/imagefolder/Main_pic.png"];
[theLists setImg:image];
}
Where am doing wrong? how to get the image as output in UIImageView of iphone
Edited:
"theLists" is a object of the file where i had defined the UIImage Object say i have a file "list.h" like this, where i define the image
#interface list : NSObject
#property(nonatomic,retain) UIImage *img;
Now in the appdelegate.m file am importing the "list.h" and use like this,
#property(nonatomic,retain) list *theLists;
and this "thelists" is used in viewDidLoad()
Edited-2
My parsing code is here,
NSString *path = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:#"sample.xml"];
NSData *data = [[NSData alloc] initWithContentsOfFile:path];
NSXMLParser *xmlParser = [[NSXMLParser alloc] initWithData:data];
egsParser *theParser =[[egsParser alloc] initParser];
[xmlParser setDelegate:theParser];
If your images is stored in array you should use something like this code:
UIImage *image = [UIImage imageWithContentsOfFile:[NSString stringWithFormat:#"%#",(((list *)[yourImagesArray objectAtIndex:i]).img)]];
To access your data if you storing it as NSString. i-number of image in array
or
UIImage *image = (((list *)[yourImagesArray objectAtIndex:i]).img);
If you getting it like UIImage
Related
When trying to Parse the XML file from remote web server, I can parse the data from XML file successfully. From the XML file i tried to retrieve an image URL and assign it to a UIImageView for a view controller. here am getting the following Error,
ARC Semantic Issue: No known class method for selector 'imageWithContentsOfUrl:'
Sematic Issue: Incompatible pointer types sending 'NSURL *__strong' to parameter of type 'NSString *'
Below is my code,
//My View Controller
[super viewDidLoad];
NSURL *url = [NSURL URLWithString:#"http://sample.com/Products.xml"];
EGSParser *parser = [[EGSParser alloc] init];
// Error in below line( here firstImage is a UIImageView property)
self.firstImage.image = [UIImage imageWithContentsOfUrl:[NSURL URLWithString:url]];
if ([parser loadXMLByURL:url]) {
NSLog(#"success; products=%#",parser.products);
self.xmlProductsResults = parser.products;
}
else
{
NSLog(#"Log Fail");
}
Kindly Suggest me an idea to solve this.
Try this :-
Replace
self.firstImage.image = [UIImage imageWithContentsOfUrl:[NSURL URLWithString:url]];
with
NSData *mydata = [[NSData alloc] initWithContentsOfURL:url];
self.firstImage.image = [UIImage imageWithData:mydata];
Hope it helps you..
Change this line :
self.firstImage.image = [UIImage imageWithContentsOfUrl:[NSURL URLWithString:url]];
to
self.firstImage.image = [UIImage imageWithContentsOfFile:[NSString stringWithFormat:#"%#",url]];
You have a typo, it is imageWithContentsOfURL: (capital URL) and not imageWithContentsOfUrl:. That's why the class method is not recognized.
Edit: also, it is a method of CIImage and not UIImage.
Should work, just replace
self.firstImage.image = [UIImage imageWithContentsOfUrl:[NSURL URLWithString:url]] ;
with
self.firstImage.image = [UIImage imageWithData:[NSData dataWithContentsOfURL:url]];
NB: the NSUrl url should be an image url
I have a window in which I want to display an image in a specific block of that window. The image is retrieved from the local XML file. I parsed the XML file and got the image value in the log, but I don't know how to put it in that specific block. The below image pasted for the reference.
The arrow mark represents that within this UIImageView I need to get the image. Also, I doubt that I need to mention any name for reference to UIIMageView in storyboard. Suggest me an idea.
Edited
My parser code:
NSString *path = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:#"Sample.xml"];
NSData *data = [[NSData alloc] initWithContentsOfFile:path];
NSXMLParser *xmlParser = [[NSXMLParser alloc] initWithData:data];
[xmlParser setDelegate:theParser]
After this I am using this code for getting image:
UIImage *image = [UIImage imageWithContentsOfFile:#"/Users/administrator/Desktop/imageApp/resources/Main_pic1.png"];
After this I don't know how to continue.
Yes, you have to create IBOutlet in .h file
like IBOutlet UIImageView *imgview; and connect imgview with your UIImageview from xib.
After that you can set image to image view.
NSURL *URL = [NSURL URLWithString:[your url which u get form local xml]];
NSData *imageData = [NSData dataWithContentsOfURL:URL];
UIImage *image = [UIImage imageWithData:imageData];
imgview.image=image;
Ttry this, It may help you.
You can directly pass the url image to Uiimageview by :
YourImageView.image = [UIImage imageWithContentsOfURL:YourUrl];
It's possible to do the equivalent by going through NSData, but it's a bad idea. Instead, the image should be downloaded asynchronously using NSURLConnection, and only once it's fully downloaded should it be converted into a UIImage and assigned to the image view.
Save all your urls in an say NSMutableArry *UrlArray then
int x = 0;
int y = 10;
for (int i = 0; i< [UrlArray]; i++)
{
UIImageView *imageView = [[[UIImageView alloc]initWithFrame:CGRectMake(3+x,y, 80,
80)]autorelease];
imageView.backgroundColor = [UIColor blueColor];
if (x >= 300)
{
x = 0;
y = y+imageView.frame.size.height+3;
imageView.frame = CGRectMake(x+3 , y, 80, 80);
}
CIImage *beginImage = [CIImage imageWithContentsOfURL:[UrlArray ObjectAtIndex:i]];
imageView.image = [UIImage imageWithCIImage:beginImage];
x = x+imageView.frame.size.width+5;
[self.view addSubview:imageView];
}
will do the work i guess;
I am new to this technology.
I searched a lot but cant find any relevant.
In my application,I am receiving byte array from web service, my byte array which I receive from web service is
[137,80,78,71,13,10,26,10,0,0,0,13,73,72,68,82,0,0,1,195,0,0,1,195,8,2,0,0,0,215,2... ]
and I want to convert this byte array into UIImage for showing it in UIImageView.
Use below constructor for UIImage.
+ (UIImage *)imageWithData:(NSData *)data;
NSData *data = [NSData dataWithBytes:YOUR_BYTE_ARRAY length:ARRAY_LENGTH];
UIImage *img = [UIImage imageWithData:data];
UIImageView *imgView = [[UIImageView alloc]initWithImage:img];
The first 8 bytes in the byte array above, \211 P N G \r \n \032 \n (or 137,80,78,71,13,10,26,10 in decimal), reveal this to be a PNG file.
At the very least, you should be able to just save your entire byte sequence to a file, and load it using + (UIImage *)imageNamed:(NSString *)name or + (UIImage *)imageWithContentsOfFile:(NSString *)path. For example:
UIImage *myImage = [UIImage imageNamed:#"myfile.png"]
// myfile.png should be in the main bundle
(Apurv's method is more direct, and better for this reason. But since you are having such difficulty with it, I thought I'd suggest a slightly different approach.)
You need to Base64 decode the data first. Data that is returned from many SOAP web services is base 64 encoded as well as sometimes raw data embedded in websites. It is pretty simple to do but just easiest to use a library to someone else has created.
Start by including this in your project and including the .h in this file: https://github.com/nicklockwood/Base64
NSString *base64String = #"**YOUR BYE ARRAY HERE**";
UIImage *imageOrig = [UIImage imageWithData:[NSData dataFromBase64String:base64String]];
UIImageView *imageView = [[UIImageView alloc]initWithImage:imageOrig];
That should do it. In my previous experiences I just put what ever data blob I get over the webservice into a string then create the image using this method and it works great there is a great discussion on the details of Base64 encoding and decoding here : http://cocoadev.com/wiki/BaseSixtyFour which is what I used to create my class but Nick's code on gitHub is much better as its ARC compliant.
From Webservice we get array of NSNumber. We will have to convert it to NSData like this:
NSMutableData *data = [[NSMutableData alloc] initWithCapacity: [strings count]];
for( NSNumber *number in strings) {
char byte = [number charValue];
[data appendBytes: &byte length: 1];
}
Covert NSData to UIImage:
UIImage *imageOrig = [UIImage imageWithData:data];
We get JSON also out of NSData.
NSError *error1 = nil;
NSArray *jsonArray = [NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:&error1];
if (error1 != nil) {
NSLog(#"Error parsing JSON.");
} else {
NSLog(#"Array: %#", jsonArray);
}
//Use this
CGColorSpaceRef colorSpace=CGColorSpaceCreateDeviceRGB();
CGContextRef bitmapContext=CGBitmapContextCreate(YOUR_BYTE_ARRAY, w, h, 8, 4*w, colorSpace, kCGImageAlphaPremultipliedLast | kCGBitmapByteOrderDefault);
CFRelease(colorSpace);
free(YOUR_BYTE_ARRAY);
CGImageRef cgImage=CGBitmapContextCreateImage(bitmapContext);
CGContextRelease(bitmapContext);
UIImage *newimage = [UIImage imageWithCGImage:cgImage];
[yourImageView setImage:newimage];
CGImageRelease(cgImage);
May be it will help you...
#import "NSDataAdditions.h"
NSData *dataObj = [NSData dataWithBase64EncodedString:StringImage];
UIImage *Image = [UIImage imageWithData:dataObj];
I have array of image url. I want to show the images into UIImageView.
Now I convert the URL to NSData and then convert that into UIImage and then try to load that into UIImageView.
But it takes a lot of time to do this.
Is there a better way where in I can load the images in a faster and better manner?
Despite all of the answers on here telling you to do this in one line of code, it will sadly make no difference to the URL connection speed OR data / image decoding. If you want a faster way to TYPE the code then fine, but I would use category added to UIImageView....
#interface UIImageView (URL)
- (void)loadFromUrl:(NSString *)aUrl;
#end
#implementation UIImageView (URL)
- (void)loadFromUrl:(NSString *)aUrl {
NSURL *url = [NSURL urlWithString:aUrl];
NSData *data = [NSData dataWithContentsOfURL:url]
UIImage *image = [UIImage imageWithData:data];
if(image != nil) {
[self setImage:image];
}
}
#end
Now you can include the header and do...
[myImageView loadFromUrl:#"http://myurl.com/image.jpg"];
For more categories (I will be adding this one to my list!) check here. Those are all my useful ones, you may find them useful too! :)
Use everything in a single statement.
UIImage *image = [UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:MyURL]]];
[UIImage imageWithData:[NSData dataWithContentsOfURL:]]
UIImage *image = [UIImage imageWithData:[NSData dataWithContentsOfURL:imageUrl]];
try this:-
image is UIImage and imageView is UIImageView
NSData *receivedData = [NSData dataWithContentsOfURL:#"yoururl"];
self.image=nil;
UIImage *img = [[UIImage alloc] initWithData:receivedData ];
self.image = img;
[img release];
[self.imageView setImage:self.image];
I'm populating the animationImages field of my UIImageview via NSArray arrayWithObjects. When I put objects in the array via UIImage imageNamed: the UIImageview animates as expected. However when I put objects in the array via UIImage alloc initWithContentsOfFile I don't see the animated images (I just see the default image I pass to initWithImage for the UIImageview). Any ideas?
Cheers!
That is because imageNamed: and initWithContentsOfFile: both look in different directories. imageNamed: looks in your bundle directory, initWithContentsOfFile: does not.
If you have code like this:
NSString *file = #"image.png";
UIImage *image = [UIImage imageNamed:file];
But you want to use initWithContentsOfFile:, you should use this code:
NSString *file = [[NSBundle mainBundle] pathForResource:#"image" ofType:#"png"];
UIImage *image = [[UIImage alloc] initWithContentsOfFile:file];