How to convert data to image in iOS - ios

NSData *imageData = UIImagePNGRepresentation(your_image_here);
After that i transferred it to another iPhone using bluetooth.
Now i need to convert the data back to image. Can any one tell me how to do it?

I assume this is to et back the data which you encoded in the previous question.
For converting UIImage to NSData ,
NSData* pictureData = UIImagePNGRepresentation(image);
To get it back,
UIImage *image = [[UIImage alloc]initWithData:pictureData];
Remember to add [image release] at the end.
or you can use
UIImage *image = [[UIImage alloc]init];
[image imageWithData:pictureData];

In swift-3 : Data is converted back to image by :--
func compressImage() -> UIImage {
let imageData = UIImageJPEGRepresentation(UIImage(named:"background.jpg")!, 0.2)
let image = UIImage(data: imageData!)
return image!
}

Just call imageWithData: or on instantiation, you can call initWithData:

[UIImage imageWithData:(NSData *)data];

Related

Loading an image from bytes in Objective-C

I'm working with an existing code base that accepts bytes from a loaded jpeg file and creates a UIImage object. That works fine on iphone but macOS needs a different implementation from what I've understood.
UIImage* image = [UIImage imageWithData:[NSData dataWithBytes:data length:length]];
if (image)
{
}
What is the equivalent of this on macOS? is it NsImage? How do I implement this?
Here it is
NSImage* image = [[NSImage alloc] initWithData:[NSData dataWithBytes:data length:length]];
if (image)
{
}

how to send the xml parsing image data to ImageView.image

I am working on parsing, I got the image data form the XML like Link
then i sent this to detail view controller by using shared delegate.
AppDelegate *sharedDelegate=(AppDelegate*)[[UIApplication sharedApplication]delegate];
imageview.image=[UIImage imageNamed:sharedDelegate.imagObj];
I got data in sharedDelegate.image but Imageview.image got only the null value.
Please help me.
If you are getting a string, then use this code :
AppDelegate *sharedDelegate=(AppDelegate*)[[UIApplication sharedApplication]delegate];
NSData * imageData = [[NSData alloc] initWithContentsOfURL: [NSURL URLWithString: [NSString stringWithFormat:#"%#",sharedDelegate.imagObj]]];
imageview.image = [UIImage imageWithData: imageData]];

Get thumbnail from ALAssetsRepresentation as NSData

I am writing NSData to a file and saving it in the device's app documents folder. For that, is it possible to get thumbnail from ALAssetsRepresentation object in NSData format. If so, any helpful links to that?
I couldn't find anything similar, other than getting CGImageRef from ALAssetsRepresentation. I don't want CGImageRef format as I have to use UIImageJPEGRepresentation or UIImagePNGRepresentation to convert it to NSData.
Try this one
GImageRef iref = [myasset thumbnail];
if (iref)
{
UIImage *theThumbnail = [UIImage imageWithCGImage:iref];
NSData *thumnailData = UIImagePNGRepresentation(theThumbnail);
}

Save Image From UIWebView into Photo Library in iOS

I want to save image from UIWebView into Photo Library in iOS app.
I also read [http://stackoverflow.com/questions/6766671/how-to-saving-uiwebview-content-into-photo-library][1]
But i can't save.
i wrote following codes in save method.
- (IBAction)saveImage:(id)sender
{
NSString *sizeOfImageByJs = [NSString stringWithFormat:#"document.body.scrollWidth;"
"document.body.scrollHeight;"
"window.innerWidth;"
"window.innterHeight;"];
NSString *urlToSave = [webViewOfPhoto stringByEvaluatingJavaScriptFromString:sizeOfImageByJs];
NSURL *imageUrl = [NSURL URLWithString:urlToSave];
NSData *imageData = [NSData dataWithContentsOfURL:imageUrl];
UIImage *image = [UIImage imageWithData:imageData];
UIImageWriteToSavedPhotosAlbum(image, self, nil, nil);
}
It's doesn't save anythings.
How to save it?
Thanks.

Can I load a UIImage from a URL?

I have a URL for an image (got it from UIImagePickerController) but I no longer have the image in memory (the URL was saved from a previous run of the app). Can I reload the UIImage from the URL again?
I see that UIImage has a imageWithContentsOfFile: but I have a URL. Can I use NSData's dataWithContentsOfURL: to read the URL?
EDIT1
based on #Daniel's answer I tried the following code but it doesn't work...
NSLog(#"%s %#", __PRETTY_FUNCTION__, photoURL);
if (photoURL) {
NSURL* aURL = [NSURL URLWithString:photoURL];
NSData* data = [[NSData alloc] initWithContentsOfURL:aURL];
self.photoImage = [UIImage imageWithData:data];
[data release];
}
When I ran it the console shows:
-[PhotoBox willMoveToWindow:] file://localhost/Users/gary/Library/Application%20Support/iPhone%20Simulator/3.2/Media/DCIM/100APPLE/IMG_0004.JPG
*** -[NSURL length]: unrecognized selector sent to instance 0x536fbe0
*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '*** -[NSURL length]: unrecognized selector sent to instance 0x536fbe0'
Looking at the call stack, I'm calling URLWithString, which calls URLWithString:relativeToURL:, then initWithString:relativeToURL:, then _CFStringIsLegalURLString, then CFStringGetLength, then forwarding_prep_0, then forwarding, then -[NSObject doesNotRecognizeSelector].
Any ideas why my NSString (photoURL's address is 0x536fbe0) doesn't respond to length? Why does it say it doesn't respond to -[NSURL length]? Doesn't it know that param is an NSString, not a NSURL?
EDIT2
OK, the only problem with the code is the string to URL conversion. If I hardcode the string, everything else works fine. So something is wrong with my NSString and if I can't figure it out, I guess that should go in as a different question. With this line inserted (I pasted the path from the console log above), it works fine:
photoURL = #"file://localhost/Users/gary/Library/Application%20Support/iPhone%20Simulator/3.2/Media/DCIM/100APPLE/IMG_0004.JPG";
You can do it this way (synchronously, but compact):
UIImage *image = [UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:MyURL]]];
A much better approach is to use Apple's LazyTableImages to preserve interactivity.
You can try SDWebImage, it provides:
Asynchronous loading
Caching for offline use
Place holder image to appear while loading
Works well with UITableView
Quick example:
[cell.imageView setImageWithURL:[NSURL URLWithString:#"http://www.domain.com/path/to/image.jpg"] placeholderImage:[UIImage imageNamed:#"placeholder.png"]];
And the swift version :
let url = NSURL.URLWithString("http://live-wallpaper.net/iphone/img/app/i/p/iphone-4s-wallpapers-mobile-backgrounds-dark_2466f886de3472ef1fa968033f1da3e1_raw_1087fae1932cec8837695934b7eb1250_raw.jpg");
var err: NSError?
var imageData :NSData = NSData.dataWithContentsOfURL(url,options: NSDataReadingOptions.DataReadingMappedIfSafe, error: &err)
var bgImage = UIImage(data:imageData)
If you're really, absolutely positively sure that the NSURL is a file url, i.e. [url isFileURL] is guaranteed to return true in your case, then you can simply use:
[UIImage imageWithContentsOfFile:url.path]
get DLImageLoader and try folowing code
[DLImageLoader loadImageFromURL:imageURL
completed:^(NSError *error, NSData *imgData) {
imageView.image = [UIImage imageWithData:imgData];
[imageView setContentMode:UIViewContentModeCenter];
}];
Another typical real-world example of using DLImageLoader, which may help someone...
PFObject *aFacebookUser = [self.fbFriends objectAtIndex:thisRow];
NSString *facebookImageURL = [NSString stringWithFormat:
#"http://graph.facebook.com/%#/picture?type=large",
[aFacebookUser objectForKey:#"id"] ];
__weak UIImageView *loadMe = self.userSmallAvatarImage;
// ~~note~~ you my, but usually DO NOT, want a weak ref
[DLImageLoader loadImageFromURL:facebookImageURL
completed:^(NSError *error, NSData *imgData)
{
if ( loadMe == nil ) return;
if (error == nil)
{
UIImage *image = [UIImage imageWithData:imgData];
image = [image ourImageScaler];
loadMe.image = image;
}
else
{
// an error when loading the image from the net
}
}];
As I mention above another great library to consider these days is Haneke (unfortunately it's not as lightweight).
Try this code, you can set loading image with it, so the users knows that your app is loading an image from url:
UIImageView *yourImageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"loading.png"]];
[yourImageView setContentMode:UIViewContentModeScaleAspectFit];
//Request image data from the URL:
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
NSData *imgData = [NSData dataWithContentsOfURL:[NSURL URLWithString:#"http://yourdomain.com/yourimg.png"]];
dispatch_async(dispatch_get_main_queue(), ^{
if (imgData)
{
//Load the data into an UIImage:
UIImage *image = [UIImage imageWithData:imgData];
//Check if your image loaded successfully:
if (image)
{
yourImageView.image = image;
}
else
{
//Failed to load the data into an UIImage:
yourImageView.image = [UIImage imageNamed:#"no-data-image.png"];
}
}
else
{
//Failed to get the image data:
yourImageView.image = [UIImage imageNamed:#"no-data-image.png"];
}
});
});
Check out the AsyncImageView provided over here. Some good example code, and might even be usable right "out of the box" for you.
AFNetworking provides async image loading into a UIImageView with placeholder support. It also supports async networking for working with APIs in general.
Make sure enable this settings from iOS 9:
App Transport Security Settings in Info.plist to ensure loading image from URL so that it will allow download image and set it.
And write this code:
NSURL *url = [[NSURL alloc]initWithString:#"http://feelgrafix.com/data/images/images-1.jpg"];
NSData *data =[NSData dataWithContentsOfURL:url];
quickViewImage.image = [UIImage imageWithData:data];
The way using a Swift Extension to UIImageView (source code here):
Creating Computed Property for Associated UIActivityIndicatorView
import Foundation
import UIKit
import ObjectiveC
private var activityIndicatorAssociationKey: UInt8 = 0
extension UIImageView {
//Associated Object as Computed Property
var activityIndicator: UIActivityIndicatorView! {
get {
return objc_getAssociatedObject(self, &activityIndicatorAssociationKey) as? UIActivityIndicatorView
}
set(newValue) {
objc_setAssociatedObject(self, &activityIndicatorAssociationKey, newValue, UInt(OBJC_ASSOCIATION_RETAIN))
}
}
private func ensureActivityIndicatorIsAnimating() {
if (self.activityIndicator == nil) {
self.activityIndicator = UIActivityIndicatorView(activityIndicatorStyle: UIActivityIndicatorViewStyle.Gray)
self.activityIndicator.hidesWhenStopped = true
let size = self.frame.size;
self.activityIndicator.center = CGPoint(x: size.width/2, y: size.height/2);
NSOperationQueue.mainQueue().addOperationWithBlock({ () -> Void in
self.addSubview(self.activityIndicator)
self.activityIndicator.startAnimating()
})
}
}
Custom Initializer and Setter
convenience init(URL: NSURL, errorImage: UIImage? = nil) {
self.init()
self.setImageFromURL(URL)
}
func setImageFromURL(URL: NSURL, errorImage: UIImage? = nil) {
self.ensureActivityIndicatorIsAnimating()
let downloadTask = NSURLSession.sharedSession().dataTaskWithURL(URL) {(data, response, error) in
if (error == nil) {
NSOperationQueue.mainQueue().addOperationWithBlock({ () -> Void in
self.activityIndicator.stopAnimating()
self.image = UIImage(data: data)
})
}
else {
self.image = errorImage
}
}
downloadTask.resume()
}
}
The Best and easy way to load Image via Url is by this Code:
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
NSData *data =[NSData dataWithContentsOfURL:[NSURL URLWithString:imgUrl]];
dispatch_async(dispatch_get_main_queue(), ^{
imgView.image= [UIImage imageWithData:data];
});
});
Replace imgUrl by your ImageURL
Replace imgView by your UIImageView.
It will load the Image in another Thread, so It will not slow down your App load.
Local URL's are super simple, just use this :
UIImage(contentsOfFile: url.path)

Resources