从解析下载图像

Posted

技术标签:

【中文标题】从解析下载图像【英文标题】:Downloading image from parse 【发布时间】:2014-10-09 17:11:16 【问题描述】:

我正在尝试从 Parse 后端下载图像。它通过创建 PFQuery、设置 Parse 类名称、可选地设置日期(将在最终版本中)和查找对象,在 UITableView 的背景中显示“今天的图像”。但是,它给了我一个错误。

    // Create a query and set it up to find todays pic
NSLocale *currentLocale = [NSLocale currentLocale];
PFQuery *todayQuery = [PFUser query];
todayQuery = [PFQuery queryWithClassName:@"Today"];
[todayQuery findObjectsInBackgroundWithBlock:^(NSArray *result, NSError *error) 
    // Download the image
    NSData *imageData;
    for (PFObject *object in result) imageData = [object objectForKey:@"todaysPic"];

    // Use the image
    UIImage *todaysPicture = [UIImage imageWithData:imageData];
    UIImage *finalTodaysPicture = [[UIImage alloc] init];
    finalTodaysPicture = [self blur:todaysPicture];
    self.tableView.backgroundColor = [UIColor colorWithPatternImage:finalTodaysPicture];
];

// Convert to JPEG with 50% quality
NSData* data = UIImageJPEGRepresentation([UIImage imageNamed:@"star.jpg"], 0.5f);
PFFile *imageFile = [PFFile fileWithName:@"Image.jpg" data:data];

这是我的 Parse 行:

Xcode 告诉我:

2014-10-09 19:05:55.496 iStory[1082:4130995] -[PFFile length]: unrecognized selector sent to instance 0x7fd18bc311f0

还有:

(lldb) bt
* thread #1: tid = 0x3f0cf1, 0x000000010c4d6b8a libobjc.A.dylib`objc_exception_throw, queue = 'com.apple.main-thread', stop reason = breakpoint 1.1
frame #0: 0x000000010c4d6b8a libobjc.A.dylib`objc_exception_throw
frame #1: 0x000000010c84450d CoreFoundation`-[NSObject(NSObject) doesNotRecognizeSelector:] + 205
frame #2: 0x000000010c79c7fc CoreFoundation`___forwarding___ + 988
frame #3: 0x000000010c79c398 CoreFoundation`__forwarding_prep_0___ + 120
frame #4: 0x000000010d077559 UIKit`_UIImageRefFromData + 57
frame #5: 0x000000010cedc55f UIKit`-[UIImage(UIImagePrivate) _initWithData:preserveScale:cache:] + 110
frame #6: 0x000000010ced89d1 UIKit`+[UIImage imageWithData:] + 50
* frame #7: 0x000000010ab937cf iStory`__37-[HomeViewController viewWillAppear:]_block_invoke(.block_descriptor=0x00007f94495a3c40, result=0x00007f9449552b80, error=0x0000000000000000) + 655 at HomeViewController.m:70
frame #8: 0x000000010abe0ac7 iStory`__49-[BFTask(.block_descriptor=<unavailable>, task=<unavailable>) thenCallBackOnMainThreadAsync:]_block_invoke + 251 at BFTask+Private.m:59
frame #9: 0x000000010abf91a8 iStory`__41-[BFTask continueWithExecutor:withBlock:]_block_invoke_2(.block_descriptor=0x00007f94495a50d0) + 30 at BFTask.m:287
frame #10: 0x000000010e651cc6 libdispatch.dylib`_dispatch_call_block_and_release + 12
frame #11: 0x000000010e66f7f4 libdispatch.dylib`_dispatch_client_callout + 8
frame #12: 0x000000010e658991 libdispatch.dylib`_dispatch_main_queue_callback_4CF + 956
frame #13: 0x000000010c7a5569 CoreFoundation`__CFRUNLOOP_IS_SERVICING_THE_MAIN_DISPATCH_QUEUE__ + 9
frame #14: 0x000000010c76846b CoreFoundation`__CFRunLoopRun + 2043
frame #15: 0x000000010c767a06 CoreFoundation`CFRunLoopRunSpecific + 470
frame #16: 0x000000010ff409f0 GraphicsServices`GSEventRunModal + 161
frame #17: 0x000000010cea6550 UIKit`UIApplicationMain + 1282
frame #18: 0x000000010ab928a3 iStory`main(argc=1, argv=0x00007fff55077370) + 115 at main.m:14
frame #19: 0x000000010e6a4145 libdyld.dylib`start + 1
(lldb)

非常感谢您的帮助!

谢谢!

真诚地, 埃里克

【问题讨论】:

【参考方案1】:

我是这样解决的:

-(void)viewWillAppear:(BOOL)animated 
[super viewWillAppear:YES];
// Create a query and set it up to find todays pic
NSLocale *currentLocale = [NSLocale currentLocale];
PFQuery *todayQuery = [PFUser query];
todayQuery = [PFQuery queryWithClassName:@"Today"];
[todayQuery findObjectsInBackgroundWithBlock:^(NSArray *result, NSError *error) 
    // Download the image
    NSData *imageData;
    PFObject *imageObject;
    for (imageObject in result) 
        PFFile *imageFile = (PFFile *)[imageObject objectForKey:@"todaysPic"];
        [imageFile getDataInBackgroundWithBlock:^(NSData *resultData, NSError *error ) 
            if (!error) 
                // Create and set the image
                UIImage *todaysImage = [UIImage imageWithData:resultData];
                UIImage *blurredTodaysImage = [self blur:todaysImage];
                self.tableView.backgroundColor = [UIColor colorWithPatternImage:blurredTodaysImage ];
             else 
                // Just set a default picture
            
        ];
    
];

【讨论】:

以上是关于从解析下载图像的主要内容,如果未能解决你的问题,请参考以下文章

从Firebase下载并解析XML文件

Swift - 从解析接收图像并显示它们的问题

php 解析下载图像

解析 ios 的图像分辨率

从解析中检索图像

如何快速从解析中加载图像?