I have to make som changes to a iPhone app...
How can i change the background on a UIViewController on load (viewDidLoad) .. I have to show a .png from a URL .. Is that posible?
I have tried:
NSURL *url = [NSURL URLWithString:#"http://url_to_my_project/xmas-bg-test.png"];
NSData *data = [NSData dataWithContentsOfURL:url];
UIImage *image = [UIImage imageWithData:data];
self.view.backgroundColor = [UIColor colorWithPatternImage:image];
this is from my : "- (void)viewDidLoad" method .. Nothing happend when I start my app.. no errors ether.
I have also tried from my .. viewDidAppear
You have to create UIImageView as a background image. Remove self.view.backgroundColor = ... line and add this:
UIImageView *imageView = [[UIImageView alloc] initWithImage:image];
imageView.frame = self.view.frame;
[self.view addSubview:imageView];
[self.view sendSubviewToBack:imageView];
in viewDidLoad: method.
That's not how you add a background to a view controller. Add a new UIImageView ivar to your view controller subclass and use that.
- (void)viewDidLoad {
...
imageView.image = [UIImage imageWithData:data];
}
You have to make sure you create this image view properly. Either by adding an image view in Interface Builder and linking it to your imageView ivar, or by creating it yourself in awakeFromNib or loadView.
The other answers are correct for saying that you should be using an image view to display the image. However, you should consider making a request for the image. This way, if the request fails you can handle it by displaying a different image or what ever you want. Here's a rough example.
NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:#"http://url_to_my_project/xmas-bg-test.png"] cachePolicy:NSURLCacheStorageNotAllowed timeoutInterval:5.0];
[NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse *response, NSData *data, NSError *error) {
if (response && !error) {
UIImage *image = [UIImage imageWithData:data];
UIImageView *imageView = [[UIImageView alloc] initWithFrame:self.view.bounds];
[imageView setImage:image];
[self.view addSubview:imageView];
}
}];
Related
I am setting image in UIImageView using:
[self.imageView setImageWithURL:[NSURL URLWithString:urlString] placeholderImage:[UIImage imageNamed:#"random_image"]];
I am able to see the image on phone as expected.
However, if I use the method without placeholder argument
[self.imageView setImageWithURL:[NSURL URLWithString:urlString]];
OR pass the placeholderImage as nil
[self.imageView setImageWithURL:[NSURL URLWithString:urlString] placeholderImage:nil];
then the image is not set and my imageView is blank. I understand that placeholder image is for "representing the current state of the asset suitable for temporarily displaying in your extension’s UI.".
I don't understand how its absence should impact me the way it is impacting. Any pointers?
If you set default placeholder image then imageview gets width and night.Otherwise imageview frame is may be (0,0).
Or
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
[NSURLConnection sendAsynchronousRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:imageUrl]] queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse *response, NSData *data, NSError *error) {
ImageViewName.image = [UIImage imageWithData:data];
}];
I found the issue. I added an image view in UITableViewCell with the name imageView. Apparently, UITableViewCell already has a property with the same name. I changed the name of myImageView to cellImageView and now, I don't need the placeholder parameter anymore! Having said that, I still don't understand what happened behind the screens!
i hope these code will help u....
dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0ul);
dispatch_async(queue, ^{
UIImage *image = [UIImage imageWithData:[NSData dataWithContentsOfURL:
[NSURL URLWithString:#“image URL”]]];
dispatch_sync(dispatch_get_main_queue(), ^{
[[cell myimage] setImage:image];
}
});
});
I'm working with AFNetworking to load my images for cell placeholder. Here is my code example. (Token from here)
NSURL *url = [NSURL URLWithString:daysWeather.weatherIconURL];
NSURLRequest *request = [NSURLRequest requestWithURL:url];
UIImage *placeholderImage = [UIImage imageNamed:#"placeholder"];
__weak UITableViewCell *weakCell = cell;
[cell.imageView setImageWithURLRequest:request
placeholderImage:placeholderImage
success:^(NSURLRequest *request, NSHTTPURLResponse *response, UIImage *image) {
NSLog(#"Load placeholder img");
weakCell.imageView.image = image;
[weakCell setNeedsLayout];
} failure:nil];
return cell;
And now I have a question. Does this images load every time, when it becomes visible? As you can see I've added NSLog(#"Load placeholder img"); in success block. And I see, that this code calls every time, when cell becomes visible (after it was visible, disappeared and become visible again)
Yes success block called every time. But AFNetworking will download the image only once and saved it in cache. Once you make request again for same URL or image it will directly load image from cache if available.
Also, if you don't want to do any specific operation in block, then simply use setImageWithURL:placeholderImage.
this is my code to load my images.
NSString *urltest = test[#"images"][#"items"][i][#"url"];
url = [NSURL URLWithString:urltest];
data = [NSData dataWithContentsOfURL:url];
img = [[UIImage alloc] initWithData:data];
[self.imgView setImage:img];
I would like to know when my is loaded. How can i do that?
In XCode Debugger section (located at the bottom left of the XCode), you can check image object value by inserting breakpoint there. You can also preview its value by clicking on preview button (human eye look like button).
You can achive it by,
First write this code
[self performSelectorOnMainThread:#selector(LoadImage:) withObject:#"MY URL STRING" waitUntilDone:NO];
And then
-(void)LoadImage:(NSString *) urlString
{
/// do something when image is loading
NSURL *imgURL=[NSURL URLWithString:urlString];
NSData *imgData=[NSData dataWithContentsOfURL:imgURL];
[self performSelectorInBackground:#selector(setImage:) withObject:imgData];
}
-(void)setImage:(NSData *) imgData;
{
/// do something when image loading is over
myImageView.image=[UIImage imageWithData:imgData];
}
And if you want to know your image is successfully downloaded or not then use following code
[NSURLConnection sendAsynchronousRequest:[NSURLRequest requestWithURL:[NSURL URLWithString::#"MY URL STRING"]] queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse* response, NSData* data, NSError* error){
if(error)
{
// Error Downloading image data
}
else
{
[myImageView setImage:[UIImage imageWithData:data]];
}
}];
I am using DBChooser in my application to import images from dropbox, I am getting image url like & to dasplay the image in UIImageView i have following code
UIImageView *imageView=[[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 500, 500)];
imageView.image = [UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:#"https://www.dropbox.com/s/7qey2let40eb9je/PRELIMINARY_FORM_2.jpg"]]];
[self.view addSubview:imageView];
but the image is not showing in application. please help me how to display the dropbox image in UIImageView, is it issue of https OR what .
See this reference: https://cantonbecker.com/etcetera/2014/how-to-directly-link-or-embed-dropbox-images/
Short answer: append raw=1 as querystring value to your image url
Let's try:
+ (void) downloadImage : (NSURL*) url withCallBack:(DownloadCallbackBlock)callback
{
NSURLRequest *urlRequest = [NSURLRequest requestWithURL:url];
NSOperationQueue *queue = [[NSOperationQueue alloc] init];
[NSURLConnection sendAsynchronousRequest:urlRequest queue:queue completionHandler:^(NSURLResponse *response, NSData *data, NSError *error)
{
NSHTTPURLResponse *httpresponse = (NSHTTPURLResponse *)response;
if (httpresponse.statusCode == 200)
{
callback(error,data);
}
else
{
//error
}
}];
}
And I use it:
[DownloadManager downloadImage:url withCallBack:^(NSError *error, NSData *data){
if (data)
{
UIImage *image = [[UIImage alloc]initWithData:data];
[_arrImages addObject:image];
dispatch_async(dispatch_get_main_queue(), ^{
[self processAddImage];
});
}
I'm making a request to a URL to download an image. I receive it as an NSData object, but when I try to convert it to a UIimage with imageWithData: the UIImage is not loaded. Here's my Code
__block ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:url];
__weak typeof(request)weakRequest = request;
__block UIImage * imager;
__block NSData * image;
[request setCompletionBlock:^{
[self parseXMLFileAtFileId:[weakRequest responseData]];
if (!files){
cell.correctImage.image = [UIImage imageNamed:#"sidebarPlaceholder"];
}
else{
NSURL *url = [NSURL URLWithString:[self downloading:[files objectAtIndex:0]]];
__block ASIHTTPRequest *requester = [ASIHTTPRequest requestWithURL:url];
__weak typeof(request)weakRequesting = requester;
[requester setCompletionBlock:^{
image = [weakRequesting responseData];
imager = [UIImage imageWithData:image];
cell.correctImage.image = imager;
[cell layoutIfNeeded];
}];
[requester startAsynchronous];
}
}];
Previously I got it working by explicitly stating the size of the UIImageView, but that should be taken care of by the UITableviewCell. How do I get the image to show up?
Try to run these block of code in main thread as it appeared you are trying to set the image and updating layout in background thread. Use following NSThread method right after
UIImage *imager = [UIImage imageWithData:image]
[self performSelectorOnMainThread:#selector(updateImage:) withObject:imager waitUntilDone:false];
// Define method to update the image
- (void)updateImage: (UIImage *)paramImage {
cell.correctImage.image = paramImage;
[cell layoutIfNeeded];
}