my App requirement is to view uploaded documents from IPAD.
i am importing document from database in the form of BLOB image and passing it to IPAD and with below code i could view image.
NSString *strImage; // String has BLOB image from DB
NSData *imageData = [self dataFromBase64EncodedString:strImage];
UIImage *image = [UIImage imageWithData:imageData];
UIImageView *m_oImageViewer = [[UIImageView alloc]init];
m_oImageViewer.image = image;
m_oImageViewer.frame=CGRectMake((self.view.frame.size.width/2)- 250,60,500,500);
[self.view addSubview:m_oImageViewer];
-(NSData *)dataFromBase64EncodedString:(NSString *)string{
if (string.length > 0)
{
NSString *data64URLString = [NSString stringWithFormat:#"data:;base64,%#", string];
NSData *data = [NSData dataWithContentsOfURL:[NSURL URLWithString:data64URLString]];
return data;
}
return nil;
If PDF file converted to BLOB why i am getting image as nil, as i can see BLOB is common so why only .png files i can se and not PDF file even after converting to BLOB.
i read support document for UIImage it does not give PDF file but i am giving BLOB image. where i am going wrong.
i have to use any other property, or this is not possible.
PDF isn't image format, its something like container with text, fonts, vector and raster images etc, so you can't convert it into UIImage, but UIWebView can show pdf (and a lot another format). I think, you should save pdf BLOB to temp folder and then show it in UIWebView, something like:
[pdfData writeToFile:tempFilePath atomically:YES];
NSURL *url = [NSURL fileURLWithPath:tempFilePath];
NSURLRequest *request = [NSURLRequest requestWithURL:url];
[self.webView loadRequest:request];
Related
I'm writing an image to the simulators cache directory using
NSURL *cacheDir = [[NSFileManager defaultManager] URLsForDirectory:NSCachesDirectory
inDomains:NSUserDomainMask].lastObject;
NSURL *avatarFileURL = [cacheDir URLByAppendingPathComponent:#"test.png" isDirectory:NO];
NSData *imageData = UIImagePNGRepresentation(avatarImage);
[imageData writeToURL:avatarFileURL atomically:YES];
This works fine and I've verified the image by logging the avatarFile url and loading it into a browser, which displays the cache image.
I then try and read the test image back and I'm getting different results if I use imageWithContentsOfFile: and imageWithData: as follows:
NSURL *cacheDir = [[NSFileManager defaultManager] URLsForDirectory:NSCachesDirectory
inDomains:NSUserDomainMask].lastObject;
NSURL *avatarFileURL = [cacheDir URLByAppendingPathComponent:#"test.png" isDirectory:NO];
// Returns a nil.
NSString *avatarFile = avatarFileUrl.absoluteString;
UIImage *image1 = [UIImage imageWithContentsOfFile:avatarFile];
// Returns the image.
NSData *imageData = [NSData dataWithContentsOfURL:avatarFileUrl];
UIImage *image2 = [UIImage imageWithData:imageData];
I'm at a loss to explain why imageWithContentsOfFile fails and imageWithData works when they are both addressing the same URL. I've verified the URL by logging it and urls are exactly the same.
Anyone know why imageWithContentsOfFile is not working?
Your issue is with this line:
NSString *avatarFile = avatarFileUrl.absoluteString;
Look at the value of avatarFile. It will be something like file:///..... imageWithContentsOfFile: expects a proper file path, not a file URL.
You need to properly convert the file URL into a path. That is done as follows:
NSString *avatarFile = avatarFileUrl.path;
http://hauwengweb.azurewebsites.net/api/AccomodationImages/images/1
I'm trying to download image on imageView. If you will paste this url on browser it will show, but on imageView it's not showing. If you will try any other image, then the same code will work, but when I used this url, the image does not show.
The image in question seems to be a WebP image (served with the wrong MIME type of image/png), which is not a format natively supported by UIImage. However, you can use iOS-WebP to decode the image:
Add the framework, header and implementation to your project, then use:
#import "UIImage+WebP.h"
NSURL *url = [NSURL URLWithString:#"http://hauwengweb.azurewebsites.net/api/AccomodationImages/images/1"];
NSData *data = [NSData dataWithContentsOfURL:url];
imageView.image = [UIImage imageWithWebPData:data];
And please remember to do the download and decoding steps asynchronously so as not to block the main UI.
Try this?
NSURL *url = [NSURL URLWithString:#"http://hauwengweb.azurewebsites.net/api/AccomodationImages/images/1"];
NSData *data = [NSData dataWithContentsOfURL:url];
self.img.image = [UIImage imageWithData:data];
Edit
The URL you provided is not an absolute path hence the data being fetched cannot be converted into an UIImage. There is something wrong with the URL or the formatting of it.
I did some research and found that I should be able to convert a PDF into a byte[] and then into a UIImage but so far no luck. The reason for not being able to use CoreGraphics is that I'm using apportable which does not support the CGPDF classes.
I'm able to create a NSData object that seems to contain the PDF but when using that object to create a UIImage the image is nil.
- (instancetype) initWithBackSize: (CGSize) size
{
NSString *path = [[ NSBundle mainBundle ] pathForResource:#"pdfTest.pdf" ofType:nil ];
NSData* pdfData = [NSData dataWithContentsOfFile:path];
UIImage* image = [[UIImage alloc] initWithData:pdfData];
CCTexture* texture = [[CCTexture alloc] initWithCGImage: image.CGImage contentScale: image.scale];
self = [super initWithTexture: texture];
return self;
}
The binary data within a PDF file cannot be translated directly into an UIImage type. You need to find a third party library or some other way of converting a PDF file into an UIImage.
Am having a set of images in document directory path,i need to convert all of my saved images into pdf with page number.am using the following code for converting pdf
UIWebView *webview=[[UIWebView alloc]initWithFrame:CGRectMake(0, 64, 320,430)];
pdfData = [PDFImageConverter convertImageToPDF: imageVIew.image
withHorizontalResolution: 550 verticalResolution: 320];
NSString *path = [NSHomeDirectory() stringByAppendingPathComponent: #"Documents/new.pdf"];
NSLog(#"path %#",path);
[pdfData writeToFile:path atomically:NO];
[webview loadRequest:[NSURLRequest requestWithURL:[NSURL fileURLWithPath:path]]];
[self.view addSubview:webview];
In this code i can able to convert a single image into pdf.How to convert all of my images into pdf ?
Here is the tutorial to generate PDF from image and other assets: http://code.tutsplus.com/tutorials/generating-pdf-documents--mobile-11265 worked for me always.
In my project i need to show the different sizes of images in zig-zag fashion. so, i converted the uiimages(url) which are coming from service to NSData and then i get the uiimage. my code is
NSURL *url = [NSURL URLWithString:[[_result objectAtIndex:i ] valueForKey:#"PImage"]];
NSData *data = [NSData dataWithContentsOfURL:url];
UIImage *image = [UIImage imageWithData:data];
so i can get the image size(width and height), But my problem is according to the image size, i need to create UIView, this code is works fine for me, but it is taking too much of time(almost 25 sec) to load 8 images. i figured converting UIImage to NSData is taking time. Is there any way to get the image size(width and height) without converting it into NSData
Thanks for spending time for me.
You can get image properties without actually loading whole image data from disk using ImageIO framework:
#import ImageIO;
...
NSURL *imageURL = … // Init URL somehow
CGImageSourceRef imgSource = CGImageSourceCreateWithURL((__bridge CFURLRef)url, NULL);
NSDictionary* imageProps = (__bridge_transfer NSDictionary*) CGImageSourceCopyPropertiesAtIndex(imgSource, 0, NULL);
NSLog(#"%#", imageProps);
CFRelease(imgSource);
Image width and height will be stored in dictionary under PixelHeight and PixelWidth keys (tested with png image, may be other image formats will use different keys)
Instead of converting url to data and to UIImage, Use EGOImageView OR AsyncImageView. You can simply pass the URL to them. Again setFrame based on size of the image.