I am developing an application in which I take NSData and convert it into image an display it in the image view. Now i want to save this data and display it again in UIImageView.
I used NSUserDefaults to save the NSData and again retrieve this data and convert into image and display it in image view.But i am not getting the image back.my code is as follows:
//for saving nsdata using nsuserdefaults method:-
NSUserDefaults *defsMVC=[NSUserDefaults standardUserDefaults];
[defsMVC setObject:stateMVC forKey:#"MapImageObject"];
//for retrieving nsdata from nsuserdefaults and display it in imageview
NSUserDefaults *defsMVC=[NSUserDefaults standardUserDefaults];
NSData *stringmapdata=[defsMVC dataForKey:#"MapImageObject"];
UIImageView *imageView=[[UIImageView alloc]initWithFrame:CGRectMake(0, 6, 320,415)];
UIImage *image = [UIImage imageWithData:stringmapdata];
[imageView setImage:image];
[image release];
[self.view addSubview:imageView];
[self.view sendSubviewToBack:imageView];
[imageView release];
I don't think you should be storing the data in the user Defaults, try storing it in a file like "yourimage.png", then you can simply do [[NSImage alloc] initWithConentsOfFile:path];
Also try using NSArchiver before saving it in the user defaults:
NSImage *image = [NSUnarchiver unarchiveObjectWithData:[[NSUserDefaults standardUserDefaults] objectForKey:#"someKey"]];
And for archiving it to the user defaults:
[[NSUserDefaults standardUserDefaults] setObject:[NSArchiver archivedDataWithRootObject:image] forKey:#"someKey"];
Gotta love Cocoa =)
My image is stored in 6th column in a form of NSData, first I have to get that data into NSData object. Than convert that NSData into image.
data = [[NSData alloc] initWithBytes:sqlite3_column_blob(statement, 6)
length:sqlite3_column_bytes(statement, 6)];
img = [UIImage imageWithData:data];
imgdata = [[NSMutableArray alloc]initWithObjects:img, nil];
Related
In my iOS7 app I have view just like profile page in any social networking site. I have a profile image in top left corner. I am fetching the image from service url and loading it to a UIImageview.
My problem is, I have to increase the size of this image view when the size of the image from service is big. And also i have to adjust other UILabels beside this image view. I am using this code to get the size of image from url:
NSURL *url = [NSURL URLWithString:path];
NSData *data = [NSData dataWithContentsOfURL:url];
UIImage *img = [[UIImage alloc] initWithData:data cache:NO];
CGSize size = img.size;
I don't know initWithData:cache: method on UIImage class, is this category thing?
Anyway you can make UIImageView dimension to fit content UIImage by [imageView sizeToFit].
If you want to adjust scale, use [UIImage imageWithData:data scale:scale].
NSString *path = #"http://www.example.com/example#x2.png"
NSURL *url = [NSURL URLWithString:path];
NSData *data = [NSData dataWithContentsOfURL:url];
UIImage *img = [[UIImage alloc] initWithData:data scale:2.0];
_imageView.image = img;
[_imageView sizeToFit];
I have my video and images stored in the NSData format in a cache.I want to retrieve the image and show it on imageController also want to retrieve video and and show it on videoController.
I can distinguish them by looking towards the file extension. But the problem is in imageController i can init image by using the NSData:
NSData *data=[NSData dataWithContentsOfURL:[NSURL fileURLWithPath:filePath isDirectory:NO]];
UIImage *image=[[UIImage alloc]initWithData:data];
But how can i use the NSData to play the video?
If you can get their path extensions, you can just use separate implementations for each type. No need to have one way down the road, eh?
-(void)example:(NSString*)filePath {
if ([filePath.pathExtension isEqualToString:#"png"]) {
NSData *data=[NSData dataWithContentsOfURL:[NSURL fileURLWithPath:filePath isDirectory:NO]];
UIImage *image=[[UIImage alloc]initWithData:data];
} else if ([filePath.pathExtension isEqualToString:#"mp4"]) {
MPMoviePlayerController *player =
[[MPMoviePlayerController alloc] initWithContentURL: [NSURL urlWithString:filePath]];
[player prepareToPlay];
[player.view setFrame: myView.bounds]; // player's frame must match parent's
[myView addSubview: player.view];
// ...
[player play];
}
}
I am trying to input the link of the image on one of the plist <string>.
What code should I add in to the implementation in order to load the image from that website and display in an UIImage?
This should work:
NSData *imgData = [[NSData alloc] initWithContentsOfURL:urlFromPlist];
UIImage *img = [UIImage imageWithData:imgData];
[img drawInRect:someRect];
- (void)requestFinished:(ASIHTTPRequest *)request
{
// Use when fetching text data
NSData *data = [NSData dataWithData:[request responseData]];
NSData *responseData = [request responseData];
UIImage *img = [[UIImage alloc ]imageWithData:data];//it blows up here
self.q.image = img;
[img release];
self.request = nil;
[delegate imageDidLoad:self.indexPathInTableView];
}
I am downloading an image data using ASIHTTP request and it worked fine. But If I try to create an image using the data from ASIHTTP request then it blows up..What is the problem?
Thanks in advance...
Its because you are using imageWithData: to create your image, you should use initWithData: or just [UIImage imageWithData:data] instead of alloc'ing first.
Your code doesn't work because imageWithData: is a static method on UIImage that generates an autoreleased UIImage instance. Because it is a static method, not an instance method, it isn't a recognised selector on the UIImage instance you get when you call [UIImage alloc]
as the error says the it's an unrecognized selector.
you should call - (id)initWithData:(NSData *)data.
[[UIImage alloc] initWithData:data];
or
[UIImage imageWithData:data];
I'm using the following code to fetch some images, that are going into a tableview. But it takes ages (5-6 seconds) to get the 30 images in.
Is there a smarter/faster way to do this?
NSString *imageUrl = ......;
NSString *urlStr =
[imageUrl UrlstringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
NSString *path = [[NSString alloc] initWithFormat:#"http://xxx.dk/xml.aspx%#", urlStr];
NSURL *url = [NSURL URLWithString:path];
NSData *data = [NSData dataWithContentsOfURL:url];
UIImage *img = [[UIImage alloc] initWithData:data];
return img;
[..... release];
Here profileimg is an get from Api ,String name
NSString *profileimg=[Check objectForKey:#"IMAGE"];
imageview.image=[[UIImage alloc] initWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:profileimg]]];
I'd rather download every new image in array of images when a cell appears on the screen and just display already saved image if a cell appears on the screen for the second time (i.e. image for this cell has been already downloaded).
use asynchronize loading of image.
check out this sample code : http://developer.apple.com/library/ios/#samplecode/LazyTableImages/Introduction/Intro.html