图像未在背景中设置

时间:2015-04-28 10:29:58

标签: ios objective-c iphone

我想在观看背景上设置图片。我正在使用此代码。

 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]];

但我得到错误

  

[UIImage length]:无法识别的选择器发送到实例0x14629ce0   2015-04-28 15:50:02.662 BoomAGift [21865:2587355] ***由于未捕获的异常'NSInvalidArgumentException'终止应用程序,原因:' - [UIImage长度]:无法识别的选择器发送到实例0x14629ce0'

3 个答案:

答案 0 :(得分:2)

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

ios-uiview-background-image

<强>目标c

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

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

<强>夫特

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

self.view.backgroundColor = UIColor(patternImage: image)

答案 1 :(得分:1)

您可以使用GCD下载异步UIImage

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]];

                       });
                   });

希望这对你有所帮助。

答案 2 :(得分:-1)

试试这个。

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];`