gpt4 book ai didi

ios - 将数据传递给 Singleton iOS

转载 作者:行者123 更新时间:2023-11-28 18:18:34 24 4
gpt4 key购买 nike

我目前在从 ViewController 传输到 PFFile 的子类时遇到数据丢失的问题。传递的数据是要上传到用户配置文件的图像数据。下面是选择图片的代码:

- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
// Access the uncropped image from info dictionary
UIImage *image = [info objectForKey:@"UIImagePickerControllerOriginalImage"];

// Dismiss controller
[picker dismissViewControllerAnimated:YES completion:nil];

// Resize image
_focusedImage.image = image;


NSData *imageData = UIImageJPEGRepresentation(image, 0.05f);
PFFile *imageFile = [PFFile fileWithName:@"Image.jpg" data:imageData];
[[imageUpload uploadImage] setImagePFFile:imageFile];
}

此 View 中 imageFile 上的日志打印正确。但是,当我将数据传递到我的单例类时 imageUpload uploadImage 这就是数据结构的样子:

+ (imageUpload *) uploadImage
{
static imageUpload*_sharedImageUpload = nil;
_sharedImageUpload = [[self alloc] init];
_sharedImageUpload.imageData = [[NSData alloc] init];

PFUser *user = [PFUser currentUser];
_sharedImageUpload.imagePFFile = [[PFFile alloc] init];

PFFile *imageFile = [PFFile fileWithName:@"Image.jpg" data:_sharedImageUpload.imageData];


[imageFile saveInBackgroundWithBlock:^(BOOL succeeded, NSError *error) {
if (!error)
{
[user setObject:imageFile forKey:@"image"];
[user saveInBackgroundWithBlock:^(BOOL succeeded, NSError *error) {
if (!error)
{
NSLog(@"This should be the profile image upload");
}
else
{
NSLog(@"Something went wrong: %@", error);
}
}];
}
}];

return _sharedImageUpload;
}

当我到达这一点时,系统只是将一个空白文件(零字节)上传到 Parse。命名是正确的,它在数据库中的正确位置,但文件本身中的数据在某处丢失了。我不知道为什么。有人有什么建议吗?

最佳答案

看起来您混淆了对象和方法。你想要的是一个单例对象,它有一个上传你的图像的方法/函数。我想这就是您要找的:

//ImageUploader.h

#import <Foundation/Foundation.h>

@interface ImageUploader : NSObject
+ (instancetype)uploader;
- (void)uploadImageFile:(PFFile *)aFile;
@end

//ImageUploader.m

#import "ImageUploader.h"

@implementation ImageUploader
+ (instancetype)uploader {
static ImageUploader * _uploader = nil;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
_uploader = [[self alloc] init];
});

return _uploader;
}

-(void)uploadPFFile:(PFFile *)imageFile{
[imageFile saveInBackgroundWithBlock:^(BOOL succeeded, NSError *error) {
if (!error)
{
[user setObject:imageFile forKey:@"image"];
[user saveInBackgroundWithBlock:^(BOOL succeeded, NSError *error) {
if (!error)
{
NSLog(@"This should be the profile image upload");
}
else
{
NSLog(@"Something went wrong: %@", error);
}
}];
}
}];
}
@end

您可以通过调用 [[ImageUploader uploader]uploadImageFile:someFile] 来调用它。

关于ios - 将数据传递给 Singleton iOS,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26063103/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com