-1

I want to set image on view background. I am using this code.

 NSString *userString =[NSString stringWithFormat:@"http:///background/%@",myString];
 NSString *escapedDataString = [userString stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
 NSURL *imageUrl=[NSURL URLWithString:escapedDataString];


 UIImage *images=[UIImage imageWithCIImage:[CIImage imageWithContentsOfURL:imageUrl]];
 self.view.backgroundColor=[UIColor clearColor];
 self.view.backgroundColor =[UIColor colorWithPatternImage:[UIImage imageNamed:images]];

But i m getting error

[UIImage length]: unrecognized selector sent to instance 0x14629ce0 2015-04-28 15:50:02.662 BoomAGift[21865:2587355] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[UIImage length]: unrecognized selector sent to instance 0x14629ce0'

Bhavin Bhadani
  • 22,224
  • 10
  • 78
  • 108
varun
  • 317
  • 1
  • 4
  • 18

3 Answers3

2

set-a-background-image-in-your-ios-application
or how-to-set-background-image-uiview

and ios-uiview-background-image

Objective-c

 UIGraphicsBeginImageContext(self.view.frame.size);
 [[UIImage imageNamed:@"YourImage"] drawInRect:self.view.bounds];
 UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
 UIGraphicsEndImageContext();

 self.view.backgroundColor = [UIColor colorWithPatternImage:image];

Swift

UIGraphicsBeginImageContext(self.view.frame.size)
UIImage(named:"YourImage")!.drawInRect(self.view.bounds)
var image: UIImage = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()

self.view.backgroundColor = UIColor(patternImage: image)
Yuyutsu
  • 2,509
  • 22
  • 38
1

You can download asynchronous UIImage with GCD.

dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0),
                   ^{
                       NSURL *imageURL = [NSURL URLWithString:@"http://www.southampton.ac.uk/~fangohr/computing/ImageMagick/pic.png"];
                       NSData *imageData = [NSData dataWithContentsOfURL:imageURL];

                       //This is your completion handler
                       dispatch_sync(dispatch_get_main_queue(), ^{

                           self.view.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageWithData:imageData]];

                       });
                   });

Hope this help you.

Jatin Patel - JP
  • 3,725
  • 2
  • 21
  • 43
-1

Try this.

NSString *userString =@"http://www.fnordware.com/superpng/pnggrad16rgba.png";  
NSString *escapedDataString = [userString stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
NSURL *imageUrl=[NSURL URLWithString:escapedDataString];
UIImage *images=[UIImage imageWithCIImage:[CIImage imageWithContentsOfURL:imageUrl]];

UIImageView *imageView = [[UIImageView alloc] initWithImage:images];
[self.view addSubview:imageView];

UIColor *patternColor = [UIColor colorWithPatternImage:images];
UIView  *patternView = [[UIView alloc] initWithFrame:self.view.frame];
[patternView setBackgroundColor:patternColor];
[self.view addSubview:patternView];`
Ashok Londhe
  • 1,491
  • 11
  • 29