UIImage initWithContentsOfFile doesn't work - objective-c

I have question: I want to avoid [UIImage imageNamed:].
So i did:
UIImage *prodImg = [[UIImage alloc] initWithContentsOfFile:#"myimage.png"];
controller.productImg.image = prodImg;
[prodImg release];
But now the image is not shown anymore.
Does anyone know how to get that to work?

The above code is not working because correct way to do it is-
NSString *thePath = [[NSBundle mainBundle] pathForResource:#"default" ofType:#"jpeg"];
UIImage *prodImg = [UIImage imageWithContentsOfFile:thePath];
controller.productImg.image = prodImg;
[prodImg release];
;)

Related

Red Eye Correction

To add red eye correction effect, I had followed this link:
How to remove red eye from image in iPhone?
Installed above library via pods. Added following code in my class.
UIImage *redEyeImage = [UIImage imageNamed:#"Userimage"];
if (redEyeImage) {
UIImage *newRemovedRedEyeImage = [redEyeImage redEyeCorrection];
if (newRemovedRedEyeImage) {
imgView.image = newRemovedRedEyeImage;
}
}
but I didn't get the required output. Is there anything I am missing? Or is there any other library to implement this concept. Please suggest.
Thanks!
You could set breakpoints on the lines right after both if statements and then run your app and see if they're being hit (perhaps redEyeImage doesn't exist or newRemovedRedEyeImage doesn't.
Another simple way to check is:
UIImage *redEyeImage;
UIImage *newRemovedRedEyeImage;
redEyeImage = [UIImage imageNamed:#"Userimage"];
if (redEyeImage) {
newRemovedRedEyeImage = [redEyeImage redEyeCorrection];
if (newRemovedRedEyeImage) {
imgView.image = newRemovedRedEyeImage;
}
}
NSLog(#"redEyeImage: %#", (redEyeImage != nil ? #"exists" : #"nil"));
NSLog(#"newRemovedRedEyeImage: %#", (newRemovedRedEyeImage != nil ? #"exists" : #"nil"));
then run this and see what the two logs at the bottom print.
Finally I resolved this issue with following code:
UIImage *img = [UIImage imageNamed:#"images.png"];
CIImage *image = [[CIImage alloc]initWithImage:img];
NSLog(#"after ciimage: %#", kCIImageAutoAdjustEnhance);
NSDictionary *options = [NSDictionary dictionaryWithObject:#"NO" forKey:kCIImageAutoAdjustEnhance];
NSLog(#"options: %#", options);
NSArray *adjustments = [image autoAdjustmentFiltersWithOptions:options];
NSLog(#"adjustments: %# ", adjustments);
for (CIFilter * filter in adjustments)
{
[filter setValue:image forKey:kCIInputImageKey];
image = filter.outputImage;
}
CIContext *context = [CIContext contextWithOptions:nil];
CGImageRef cgImage = [context createCGImage:image fromRect:image.extent];
UIImage *enhancedImage = [[UIImage alloc] initWithCGImage:cgImage];
CGImageRelease(cgImage);
self.img.image = enhancedImage;

swift to objective C set image prom facebook login

How can I write the code below in Objective C:
let strPictureURL: String = (result.objectForKey("picture")?.objectForKey("data")?.objectForKey("url") as? String)!
self.ivUserProfileImage.image = UIImage(data: NSData(contentsOfURL: NSURL(string: strPictureURL)!)!)
I have been trying to rewrite the code in objective C and don't find a way to set an image from a NSString. So far I have managed this:
NSString *strPictureURL = [[[result objectForKey:#"picture"] objectForKey: #"data"] objectForKey:#"url"];
NSString *strPictureURL = [[[result objectForKey:#"picture"] objectForKey: #"data"] objectForKey:#"url"];
self.ivUserProfileImage.image = [UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:strPictureUrl]]];
I managed to make it work like this:
NSString *strPictureURL = [[[result objectForKey:#"picture"] objectForKey: #"data"] objectForKey:#"url"];
UIImage *im = [UIImage imageWithData: [NSData dataWithContentsOfURL:[NSURL URLWithString:strPictureURL]]];
[self.imageProfilePic setImage:im];

Array with UIImageView elements - setImageWithURL

I have such code
arrayWithImages = [[NSMutableArray alloc] init];
NSEnumerator *enumForNames = [arrayWithNames objectEnumerator];
NSEnumerator *enumForURLs = [arrayWithURLs objectEnumerator];
id objName, objURL;
while(objName = [enumForNames nextObject]) {
objURL = [enumForURLs nextObject];
UIImageView *anImage = nil;
[anImage setImageWithURL:[NSURL URLWithString:objURL]];
(...)
[arrayWithImages addObject:anImage];
}
And each time I got SIGABRT in line with "[arrayWithImages addObject:anImage];"
What's here wrong?
I don’t see an setImageWithURL method on UIImageView. Where is this from?
Is there any output from the SIGABRT crash?
Try this code:
// Download the image
NSData *imageData = [NSData dataWithContentsOfURL:[NSURL URLWithString:objURL]];
// Make an UIImage out of it
UIImage *anImage = [UIImage imageWithData:imageData];
// Create an image view with the image
UIImageView *imageView = [[UIImageView alloc] initWithImage:anImage];
// Check to make sure it exists
if (imageView != nil) {
// Add it to your array
[arrayWithImages addObject:imageView];
else {
NSLog(#"Image view is nil");
}
Note that you should download the images asynchronously to avoid hanging the loop. This blog post discussing asynchronous image downloading.
Also if you know that [enumForURLs nextObject]; will return an NSString (or even better, NSURL) you should assign objURL to type NSString (or NSURL).

iOS load an image based on user input

fairly new to Objective-C and iOS development (coming from PHP) and I have a relatively simple question that I can't seem to find an answer to:
I am following along with an example for split View design where a web page is loaded into the Detail View when a user clicks an item in the master view. I got all this working, but would like to substitute web view for an image. So I've amended the app to load a UIImage instead of a WebView. What I'm looking for is the equivalent to this code:
NSString *urlString = [pagesAddress objectAtIndex:indexPath.row];
NSURL *url = [NSURL URLWithString:urlString];
// these 2 is where I get lost with the images.
NSURLRequest = *request = [NSURLRequest requestWithURL:url];
[detailViewController.webView loadRequest:request];
I came up with this:
NSString *imageName = [pagesAddress objectAtIndex:indexPath.row];
UIImage *myImage = [UIImage imageNamed:imageName];
// missing the last 2 calls: one to tell Xcode that it's an image "request" I want and the second to load the actual image (based on it's name that is already in an array) into the ImageView.
Thanks.
PS
I tried this:
NSString *imageName = [pagesAddress objectAtIndex:indexPath .row];
[detailViewController.imageView setImage:[UIImage imageNamed:imageName]];
And it shows just the first image, then crashes when I try to show the last one.
In the end, the solution were those 2 lines when I amended the code:
NSString *imageName = [pagesAddress objectAtIndex:indexPath.row];
[detailViewController.imageView setImage:[UIImage imageNamed:imageName]];
Notice that I had to change the setImage to convert the NSString to a UIImage or Xcode would complain. It turns out it was crashing because in the array where I had the image names, I had put 3 images into one entry (basically I forgot the commas!) so it was out of range.
Tim:
This line you gave me
UIImageView *imageView = [[UIImageView alloc] initWithFrame:self.view.bounds];
is unnecessary because I already have a view created, it would create another view which I never used. Also, replacing it with CGRect seems overkill if I already have a UIImage placeholder no?
In any case, it works now and I'm very grateful for all the help. iPad development with Objectve-C is a very thorny road and I expect I'll be bugging you guys some more.
Cheers.
Try this:
UIImage *myImage = [[UIImage alloc] initWithData:[NSData dataWithConentsOfURL:[NSURL URLWithString:urlString];
// don't know if you already got the following?
UIImageView *imageView = [[UIImageView alloc] initWithFrame:self.view.bounds];
[imageView setImage:myImage];
[self.view addSubview:imageView];
The first line is synchronous (= blocking), so in production, you should rather use - [NSURLRequest start] for this (but that's a bit more complicated).
Or use this for your local images:
UIImage *myImage = [UIImage imageNamed:imageName];
// Now, follow the same steps as in the first code-example, just skip the first line.
Try this (on iOS 4.0 and later):
// Execute a block of code on a background thread.
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0),
^(void)
{
NSAutoreleasePool* pool = [[NSAutoreleasePool alloc] init];
UIImage* image = [UIImage imageWithData:[NSData dataWithContentsOfURL:url]];
// When IO is done and image created, set it on the main thread.
dispatch_async(dispatch_get_main_queue(),
^(void)
{
imageView.image = image;
});
[pool release];
});

Thumbnail from NSURL

I am loading files that are downloaded from the web in a UIWebView (HTML files).
Is there a way to create a UIImage from an HTML file located in the Documents folder?
The goal is to auto generate thumbnails without needing to manual create them...
Update
My train of thought would be to do something in the line of:
NSString * s = [[NSBundle mainBundle] pathForResource:#"test" ofType:#"html"];
UIImage *image = [[UIImage alloc] initWithData:
[NSData dataWithContentsOfURL:url]];
Which of course does not work in this case!
Thanks!
S.
I found this solution here
UIGraphicsBeginImageContext(yourWebView.bounds.size);
[yourWebView.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage *resultImageView = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
resultImageView will be an image that contains the render of yourWebView.
If you want to shrink it down to a thumbnail size from there, I like Trevor's UIImage category for that purpose.