如何将空值分配给NSData而不是NULL值?
我不想指定null也不想指定nil.我想空值不像下面的代码.
NSData* variable = NULL; // not even assigning to nil
NSData* variable = nil;
Run Code Online (Sandbox Code Playgroud) 我有一些 NSData,可以使用 UIActivityViewController 成功添加到电子邮件中,如下所示:
NSData *pDFdada = [NSData dataWithContentsOfFile:path];
NSArray* dataToShare = @[pDFdada];
UIActivityViewController* activityViewController = [[UIActivityViewController alloc] initWithActivityItems: dataToShare applicationActivities:nil];
Run Code Online (Sandbox Code Playgroud)
现在该 pdf 已共享,但使用通用名称“Attachment-1”。我想给它一个自定义名称,例如“myNewFile.pdf”。这可能吗?
关于这个问题已有很多问题,但所给出的答案都是针对特定情况的,回答了海报的个人问题,而不是问题标题.
我想知道是否有一种通用的,通用的,简单的方法将a转换NSString为a NSDictionary,反之亦然?
我有困难/复杂/非跨平台的方法,但肯定有一个更简单的方法吗?
这是我所知道/经过测试的:
Dictionary- > String方法,只要你的Dictionary只是基本数据类型,数组和字典,它就能完美运行.这涵盖了大多数现实世界的案例,但Apple没有反过来:(.
[myDictionary description];NSData无声地失败,因此它仅适用于一组有限的实际情况:
[myString dataUsingEncoding:NSUTF8StringEncoding];[[NSString alloc] initWithData:myData encoding:NSUTF8StringEncoding]; // NB:在许多情况下默默地失败,返回nil字符串NSDictionary- > NSData- >运行NSDicitonary良好 - 但它们正在使用NSData,因此它们不是直接转换,而且它们更难以使用
[NSKeyedArchiver archivedDataWithRootObject:myDictionary];[NSKeyedUnarchiver unarchiveObjectWithData:myData];[NSPropertyListSerialization dataFromPropertyList:plist format:NSPropertyListXMLFormat_v1_0 errorDescription:&error];[NSPropertyListSerialization propertyListFromData:plistData mutabilityOption:NSPropertyListImmutable format:&format errorDescription:&error];[NSJSONSerialization JSONObjectWithData:myData options:0 error:nil];[dataWithJSONObject:myDictionary options:0 error:nil];目前,唯一可以保证工作的路线似乎是:
编写大量的代码输出冗长XML或者JSON,将其转换为NSData,然后转换回成NSString,并发送上线.
其他任何东西的成功率都略低于100%.不幸的是,这种技术将一个非常常见的问题(字符串 - …
我是 iOS 编程的新手,我刚刚学习了一些关于保存/加载对象的基础知识。在我的书中,有一个将图像保存到文件的示例:
NSData *data = UIImageJPEGRepresentation(someImage, 0.5);
[data writeToFile:imagePath atomically:YES];
Run Code Online (Sandbox Code Playgroud)
我的书还有一个将“essay”对象保存到文件的示例(“essay”对象有一个字符串作为标题,另一个字符串用于作者):
essay.m符合<NSCoding>协议:
- (void) encodeWithCoder:(NSCoder *)aCoder
{
[aCoder encodeObject:self.essayTitle forKey:@"essayTitle"];
[aCoder encodeObject:self.essayAuthor forKey:@"essayAuthor"];
}
- (instancetype) initWithCoder:(NSCoder *)aDecoder
{
self = [super init];
if (self) {
_essayTitle = [aDecoder decodeObjectForKey:@"essayTitle"];
_essayAuthor = [aDecoder decodeObjectForKey:@"essayAuthor"];
}
return self;
}
Run Code Online (Sandbox Code Playgroud)
在essayStore.m:
[NSKeyedArchiver archiveRootObject:self.myEssay toFile:somePath];
Run Code Online (Sandbox Code Playgroud)
我有三个问题:
什么时候应该使用 NSData 将对象保存到一个/多个文件,什么时候我应该遵守<NSCoding>协议将对象保存到一个/多个文件?
什么时候应该将所有对象保存到一个文件中,什么时候应该为每个对象保存一个文件?
如果我的论文对象中有图像,我如何将其与图像一起保存?
谢谢!
我有以下几行:
let jsonResults = NSData(contentsOfURL: Fetcher.URLforLicenseInfo())
Run Code Online (Sandbox Code Playgroud)
这编译并执行正常,但如果NSData的初始化失败,我会在代码中稍后获得异常.我试图添加另一行:
if jsonResults != nil { ///blah, blah...
Run Code Online (Sandbox Code Playgroud)
但随后编译器抱怨 "Cannot invoke '!=' with an argument list type '(NSData, NilLiteralConvertible)"
编译器不应该将NSData初始化返回类型识别为可选吗?
假设你从一个UIImage开始,你想要裁剪它.最常见的方法是使用CGImageCreateWithImageInRect来获取CGImageRef并从中创建一个新图像,如下所示:
CGRect cropRect = CGRectMake(x*width, y*height, width, height);
CGImageRef imageRef = CGImageCreateWithImageInRect([image CGImage], cropRect);
UIImage *croppedImage = [UIImage imageWithCGImage:imageRef];
Run Code Online (Sandbox Code Playgroud)
现在让我们说你以后需要将这个UIImage转换为NSData对象.例如,如果您希望使用NSKeyedArchiver将映像存储在磁盘上,则会发生这种情况.或者,您可以通过以下显式执行以下操作来获取NSData对象:
NSData *imageData = UIImagePNGRepresentation(croppedImage);
Run Code Online (Sandbox Code Playgroud)
我做了这个,后来我尝试通过以下方式重建一个新的UIImage对象:
UIImage *image = [UIImage imageWithData:imageData];
Run Code Online (Sandbox Code Playgroud)
我为一系列48张图片做了这个.他们中的大多数都很好,但是在每4张图片之后,接下来的两张图片都被破坏了,每张图片的顶部和底部都被交换了.如果我将转换转换为NSData并再次返回,它可以正常工作.如果我使用NSKeyedArchiver将图像数组写入磁盘(我猜它在其编码例程中调用类似UIImagePNGRepresentation的东西),我也会得到完全相同的问题.我的理论是这必须与我如何从CGImageRefs构建图像有关.
这是什么解决方法?我似乎无法弄清楚如何将CGImageRefs放入NSData对象,我更喜欢一个正确构建UIImage的解决方案,以便它可以被序列化.
编辑:
有些人要求运行实际的代码,所以我在这里添加它
UIImage *image = self.animationObject.image;
for (int y=0; y<[self.animationInfoObject.numInY intValue]; y++) {
for (int x=0; x<[self.animationInfoObject.numInX intValue]; x++) {
CGRect cropRect = CGRectMake(x*width, y*height, width, height);
CGImageRef imageRef = CGImageCreateWithImageInRect([image CGImage], cropRect);
UIImage *croppedImage = [UIImage imageWithCGImage:imageRef];
NSData *imageData = UIImageJPEGRepresentation(croppedImage, 1.0);
[self addImageToArray:imageData];
CFRelease(imageRef);
}
} …Run Code Online (Sandbox Code Playgroud) 我有一些代码创建一个名为“content”的 NSSecureCoding 变量,我想将该变量转换为 NSData,然后可以将其制作成 UIImage 或发送到本地服务器。我如何正确转换它?我希望将其用于我在 iOS 应用程序中制作的共享扩展,因此当您在照片上按共享时,它会获取照片内容并将其转换为 NSData。这是我的代码:
inputItem = extensionContext!.inputItems.first as NSExtensionItem
attachment = inputItem.attachments![0] as NSItemProvider
if (attachment.hasItemConformingToTypeIdentifier(kUTTypeImage as String)){
attachment.loadItemForTypeIdentifier(kUTTypeImage as String,
options: nil,
completionHandler: {(content, error: NSError!) in
//insert code to convert "content"(NSSecureCoding) to NSData variable
})
}
Run Code Online (Sandbox Code Playgroud) This might be an amateur question, but although I have searched Stack Overflow extensibly, I haven't been able to get an answer for my specific problem.
I was successful in creating a GIF file from an array of images by following a Github example:
func createGIF(with images: [NSImage], name: NSURL, loopCount: Int = 0, frameDelay: Double) {
let destinationURL = name
let destinationGIF = CGImageDestinationCreateWithURL(destinationURL, kUTTypeGIF, images.count, nil)!
// This dictionary controls the delay between frames
// If you don't …Run Code Online (Sandbox Code Playgroud) 我有一个应该处理下载图像的方法.除了从Firebase存储返回的数据是0字节之外,它似乎完全正常工作.为什么是这样?
func useDatabaseToDownloadPicture () {
if let userId = FIRAuth.auth()?.currentUser?.uid {
// Get a reference to the storage service using the default Firebase App
let storage = FIRStorage.storage()
let firebaseImages = FIRStorage.storage().reference().child("Images")
let userPhotoLocation = firebaseImages.child("Users").child(userId).child("profilePicture.jpg")
let myRef = FIRStorage.storage().reference()
let firebaseProfilePicLocation = firebase.child("Users").child(userId).child("Images").child("profilePicture").child("downloadURL")
firebaseProfilePicLocation.observe(.value, with: { (snapshot) in
print(" Profile Picture Databse Snapshot -> \n \(snapshot) ")
// Get download URL from snapshot
if let downloadURL = snapshot.value as? String {
print(" downloadURL converted to String ")
// Create …Run Code Online (Sandbox Code Playgroud) 我正在构建一个简单的 CoreData 应用程序。在某一时刻,用户可以使用 CoreData 以 NSData 格式上传和存储图像。保存 ManagedObjectContext 的工作方式如下:
let item = Item(context: self.managedObjectContext)
item.theImage = selectedImageFromPicker.pngData() as NSData?
//saving the MOC
Run Code Online (Sandbox Code Playgroud)
现在,当我尝试检索图像时,我面临一系列问题。
struct Box {
var id: Int
let title: String
let image: NSData?
}
struct BoxView: View {
let box: Box
let context = (UIApplication.shared.delegate as! AppDelegate).persistentContainer.viewContext
var item: Item
var body: some View {
VStack {
Image(uiImage: UIImage(data: box.image) as! Data)
.resizable()
.frame(width: 120, height: 120, alignment: .center)
.aspectRatio(contentMode: .fill)
}
}
}
Run Code Online (Sandbox Code Playgroud)
我很确定用包含数据的 UIImage 显示 Image() …
nsdata ×10
ios ×7
objective-c ×4
swift ×4
cgimageref ×1
cocoa ×1
cocoa-touch ×1
firebase ×1
gif ×1
image ×1
iphone ×1
nscoding ×1
nsdictionary ×1
nsstring ×1
swiftui ×1
uiimage ×1
xcode ×1
xcode6 ×1