我想从我的应用程序中的URL加载一个图像,所以我首先尝试使用Objective-C然后使用Swift,我有一个编译错误:
'imageWithData'不可用:使用对象构造'UIImage(data :)'
我的功能:
@IBOutlet var imageView : UIImageView
override func viewDidLoad() {
super.viewDidLoad()
var url:NSURL = NSURL.URLWithString("http://myURL/ios8.png")
var data:NSData = NSData.dataWithContentsOfURL(url, options: nil, error: nil)
imageView.image = UIImage.imageWithData(data)// Error here
}
Run Code Online (Sandbox Code Playgroud)
在Objective-C中:
- (void)viewDidLoad {
[super viewDidLoad];
NSURL *url = [NSURL URLWithString:(@"http://myURL/ios8.png")];
NSData *data = [NSData dataWithContentsOfURL:url];
_imageView.image = [UIImage imageWithData: data];
_labelURL.text = @"http://www.quentinroussat.fr/assets/img/iOS%20icon's%20Style/ios8.png";
}
Run Code Online (Sandbox Code Playgroud)
有人可以解释一下为什么imageWithData:它不适用于Swift,我该如何解决这个问题.
我一直在努力调整图像大小.基本上我已经弄明白了: 如何缩小UIImage并使其同时变得脆弱/锐利而不是模糊?
这似乎是一个合法的解决方案,但不知何故,它无法正常工作.
我的应用程序使用相机胶卷中的照片.这张照片应调整到大约200x200,而宽度很重要,而不是高度.
不幸的是,我没有示例代码,因为我在愤怒中放弃了关于非工作解决方案,对不起.
当我尝试在Swift中创建按钮并设置背景图像时:
let button = UIButton.buttonWithType(UIButtonType.System) as UIButton
button.frame = CGRectMake(100, 100, 100, 100)
button.setImage(IMAGE, forState: UIControlState.Normal)
button.addTarget(self, action: "btnTouched:", forControlEvents: UIControlEvents.TouchUpInside)
self.view.addSubview(button)
Run Code Online (Sandbox Code Playgroud)
我总是得到一个错误:" 无法将表达式的'Void'转换为'UIImage'类型 ".
我正在用Swift编写我的第一个iOS应用程序(仅限iPhone).主应用程序视图应允许用户从照片库中选择图像.
我找到了以下ViewController.swift的示例代码:
class ViewController: UIImagePickerController, UINavigationControllerDelegate, UIImagePickerControllerDelegate {
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
func tableView(tableView: UITableView!, didSelectRowAtIndexPath indexPath: NSIndexPath!) {
var imagePickerController = UIImagePickerController()
imagePickerController.delegate = self
imagePickerController.sourceType = UIImagePickerControllerSourceType.SavedPhotosAlbum
imagePickerController.allowsEditing = true
self.presentViewController(imagePickerController, animated: true, completion: { imageP in
})
}
func imagePickerController(picker: UIImagePickerController!, didFinishPickingImage image: UIImage!, editingInfo: …Run Code Online (Sandbox Code Playgroud) 我在其他帖子中看到过这段代码,用于保存图片:
// Create path.
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *filePath = [[paths objectAtIndex:0] stringByAppendingPathComponent:@"Image.png"];
// Save image.
[UIImagePNGRepresentation(image) writeToFile:filePath atomically:YES];
Run Code Online (Sandbox Code Playgroud)
我正在尝试转换为swift以保存带有avfoundatioin的图片但我不知道类型NSDocumentDirectory和NSUserDomainMask这里如何转换这个?
谢谢!!
最近我遇到了一些尝试获取 UIColor 的 CIColor 属性失败的代码。UIColor 被初始化为一个简单的 [UIColor blackColor] 并试图获得它的 CIColor 引发了异常。
CIColor *aCIColor = [[UIColor blackColor] CIColor]; //fails
Run Code Online (Sandbox Code Playgroud)
CIColor 和 UIColor 有什么区别,为什么会发生这种情况?
我对何时使用 UIImage、CIImage 和 CGImage 感到困惑
它们之间有什么区别?我什么时候可以使用它们?
ios ×6
swift ×6
uiimage ×2
avfoundation ×1
nsurl ×1
objective-c ×1
path ×1
uibutton ×1
uicolor ×1