我创建了NSMutableUrlRequest来将数据发送到服务器,向其中添加所有必需的字段,然后添加用于发送的字符串,如下所示:
[theRequest setHTTPBody:[postString dataUsingEncoding: NSUTF8StringEncoding]];
Run Code Online (Sandbox Code Playgroud)
postString是通常的NSString。
问题是,当我在服务器上收到此请求时,所有加号(+)从http正文中消失。因此,如果我在iPhone上安装了“ abcde + fghj”,则在服务器上会收到“ abcde fghj””。
使用dataUsingEncoding:NSUTF8StringEncoding会不会是一些编码问题?还是某些NSMutableUrlRequest剥离功能?我该怎么做才能使其停止剥离加号?我需要在服务器端接收UTF8字符串。
如何将空值分配给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”。这可能吗?
我是 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>协议将对象保存到一个/多个文件?
什么时候应该将所有对象保存到一个文件中,什么时候应该为每个对象保存一个文件?
如果我的论文对象中有图像,我如何将其与图像一起保存?
谢谢!
我有一些代码创建一个名为“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() …
我有一个NSImage,我试图像这样调整大小;
NSImage *capturePreviewFill = [[NSImage alloc] initWithData:previewData];
NSSize newSize;
newSize.height = 160;
newSize.width = 120;
[capturePreviewFill setScalesWhenResized:YES];
[capturePreviewFill setSize:newSize];
NSData *resizedPreviewData = [capturePreviewFill TIFFRepresentation];
resizedCaptureImageBitmapRep = [[NSBitmapImageRep alloc] initWithData:resizedPreviewData];
saveData = [resizedCaptureImageBitmapRep representationUsingType:NSJPEGFileType properties:nil];
[saveData writeToFile:@"/Users/ricky/Desktop/Photo.jpg" atomically:YES];
Run Code Online (Sandbox Code Playgroud)
我的第一个问题是,当我尝试调整大小并且不符合宽高比时,我的图像会被压扁.我读到使用-setScalesWhenResized会解决这个问题,但事实并非如此.
我的第二个问题是,当我尝试将图像写入文件时,图像实际上根本没有调整大小.
提前谢谢,瑞奇.
我是iOS的互联网连接新手.我正试图从特殊网站获取数据.
以下代码适用于所有站点以查看URL中的数据.
但是,如果我更改它的特殊网站,以获取他们的数据,它返回NULL!
我认为网站有些如何阻止这种类型的实现.因为该站点提供了一些XML信息.
NSString *URLString = @"http://www.specialsite.com/";
NSURL *postURL = [NSURL URLWithString:URLString];
NSURLRequest *postRequest = [NSURLRequest requestWithURL:postURL];
NSURLResponse *response = nil;
NSError *error = nil;
NSData *responseData = [NSURLConnection sendSynchronousRequest:postRequest returningResponse:&response error:&error];
if (responseData) {
NSLog(@"Response was %@", [NSString stringWithCString:[responseData bytes] encoding:NSUTF8StringEncoding]);
}
Run Code Online (Sandbox Code Playgroud)
控制台中的响应也是:
2011-12-26 12:24:37.245 Arz[6113:207] Response was (null)
Run Code Online (Sandbox Code Playgroud)
记得如果我改变:
NSString *URLString = @"http://www.specialsite.com/";
Run Code Online (Sandbox Code Playgroud)
至
NSString *URLString = @"http://www.apple.com/";
Run Code Online (Sandbox Code Playgroud)
工作长官!
对不起,我不能在这里(公共)说出网站的名称.