我一直在快速学习,我正在尝试开发一个下载图像的OS X应用程序.
我已经能够将我正在寻找的JSON解析为一系列URL,如下所示:
func didReceiveAPIResults(results: NSArray) {
println(results)
for link in results {
let stringLink = link as String
//Check to make sure that the string is actually pointing to a file
if stringLink.lowercaseString.rangeOfString(".jpg") != nil {2
//Convert string to url
var imgURL: NSURL = NSURL(string: stringLink)!
//Download an NSData representation of the image from URL
var request: NSURLRequest = NSURLRequest(URL: imgURL)
var urlConnection: NSURLConnection = NSURLConnection(request: request, delegate: self)!
//Make request to download URL
NSURLConnection.sendAsynchronousRequest(request, queue: NSOperationQueue.mainQueue(), completionHandler: { …Run Code Online (Sandbox Code Playgroud) 我得到的图像只有一个表示,那是一个NSCGImageSnapshotRep.
我试过[NSCGImageSnapshotRep bitmapData]但是,班级没有选择器bitmapData.
有人知道这堂课吗?我该bitmapData怎么办?
我从Webkit获得了这个NSImage [DOMElement renderedImage].
正确的用法是[NSBitmapImageRep representationUsingType:id properties:id],这种方法在这种情况下不起作用.
我也没多想compatibily,我会很高兴找到一个解决方案10.5+或10.6以上版本.
一个快速的问题.不是Obj-C问题.请不要将其标记为重复
我正在尝试编写一个在OS X上生成JPEG缩略图的类.我希望我有一些合理的东西形成缩小的NSImage,但我很难将JPEG文件写入磁盘.
class ImageThumbnail {
let size = CGSize(width: 128, height: 128)
var originalPath = ""
init(originalPath: String){
self.originalPath = originalPath
}
func generateThumbnail() {
let url = NSURL(fileURLWithPath: originalPath)
if let imageSource = CGImageSourceCreateWithURL(url, nil) {
let options: [NSString: NSObject] = [
kCGImageSourceThumbnailMaxPixelSize: max(size.width, size.height) / 2.0,
kCGImageSourceCreateThumbnailFromImageAlways: true
]
let cgImage = CGImageSourceCreateThumbnailAtIndex(imageSource, 0, options)
let scaledImage = cgImage.flatMap { NSImage(CGImage: $0, size: size) }
// How do I write scaledImage to a JPEG …Run Code Online (Sandbox Code Playgroud) 一直在争夺这一个.基本上,我正在将图像转换为NSData,因此我可以将其发送到服务器.我之前使用过的代码,但由于某种原因,我在此处收到ARC错误.错误落在我声明imageData变量的行上.
注意:myImage将传递给该方法.
- (void)uploadImage:(NSImage *)myImage {
NSData *imageData = UIImageJPEGRepresentation(myImage, 1.0);
// Do something...
}
Run Code Online (Sandbox Code Playgroud)
我收到错误和两个警告
Error: Implicit conversion of 'int' to 'NSData *' is disallowed with ARC
Warning: Implicit declaration of function 'UIImageJPEGRepresentation' is invalid in C99
Warning: Incompatible integer to pointer conversion intializing 'NSData * __strong' with an expression of type 'int'
Run Code Online (Sandbox Code Playgroud)
有任何想法吗?
我将OSX图标(NSImage)存储在核心数据中作为Transformable属性.IOS似乎无法解码NSImage.
我可以选择在Core Data中存储兼容图像,使用可转换(或其他)属性并访问iOS上的图像.
目前,用户可以将自己的图像放到NSImageWell上,并将其作为NSImage存储在objective-c类中.这显然可以归档到可转换的核心数据中,当iOS尝试检索它时会引发异常,因为它不了解NSImage对象.
我可以在OS X上使用NSImage以外的其他东西与iOS兼容吗?
cocoa ×3
objective-c ×3
macos ×2
nsimage ×2
swift ×2
ios ×1
json ×1
nsdata ×1
osx-yosemite ×1
uiimage ×1