【问题标题】:Image not getting saved (download) in iOS 4.3图像未在 iOS 4.3 中保存(下载)
【发布时间】:2013-04-11 12:25:33
【问题描述】:

我正在使用

     UIImage *viewImage = [UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL  URLWithString:self.imageURL]]];
NSLog(@"UIImage : %@",viewImage);
ALAssetsLibrary *library = [[ALAssetsLibrary alloc] init];

[library writeImageToSavedPhotosAlbum:[viewImage CGImage] orientation:(ALAssetOrientation)[viewImage imageOrientation] completionBlock:^(NSURL *assetURL, NSError *error){

if (error) {
    NSLog(@"error");
} else {
    NSLog(@"URL IS  %@", assetURL);
    [self assignDictonary:viewImage withURL:assetURL withMediaType:@"public.image"];

}
}];
[library release];

或

 dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT,(unsigned long)NULL), ^(void) {
UIImageWriteToSavedPhotosAlbum([UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:self.imageURL]]], self, @selector(image:didFinishSavingWithError:contextInfo:), nil);
});

保存图片..

iOS 5 以上的所有版本都可以正常工作...

它不适用于 iOS 4.3...

为什么会这样.. 谁能给我一些其他的解决方案..

【问题讨论】:

  • 使用的图片网址是什么?它是本地的还是远程的?如果它是远程的,则很可能您正在尝试创建图像并保存它,但在您尝试将其写入已保存的相册之前它尚未下载。
  • 它是来自服务器的 URL.. 但它在 iOS 5 及更高版本上运行良好..

标签: iphone ios objective-c ios4 iphone-sdk-3.0


【解决方案1】:

我认为问题是在您尝试调用将图像保存到资产库后加载图像。

另一种方法是使用 NSURLConnection 下载图像,而不是信任

+[NSData dataWithContentsOfURL:] 方法。这就是我在 iOS5+ 上的做法:

  NSURL *imageURL = [NSURL  URLWithString: @"https://i.chzbgr.com/maxW500/7335737088/h25EA2F17/"];
  NSURLRequest *request = [NSURLRequest requestWithURL: imageURL];
  [NSURLConnection sendAsynchronousRequest:request
                                     queue: [NSOperationQueue mainQueue]
                         completionHandler:^(NSURLResponse *response, NSData *data, NSError *error)
   {
     UIImage *viewImage = [UIImage imageWithData: data];
     ALAssetsLibrary *library = [[ALAssetsLibrary alloc] init];

     [library writeImageToSavedPhotosAlbum: [viewImage CGImage]
                               orientation: (ALAssetOrientation)[viewImage imageOrientation]
                           completionBlock:
      ^(NSURL *assetURL, NSError *error)
      {
        if (error)
        {
          NSLog(@"error");
        }
        else
        {
          NSLog(@"URL IS  %@", assetURL);
        }
      }];
     NSLog(@"UIImage : %@",viewImage);
   }];

【讨论】:

  • 这里抛出异常 "[NSURLConnection sendAsynchronousRequest:request queue: [NSOperationQueue mainQueue] completionHandler:^(NSURLResponse *response, NSData *data, NSError *error)"
  • 异常说什么?你有回溯吗?
  • 我认为 iOS 4.3 不支持此功能“[NSURLConnection sendAsynchronousRequest:request queue: [NSOperationQueue mainQueue] completionHandler:^(NSURLResponse *response, NSData *data, NSError *error) {" ..
  • 好点,iOS 4 不支持基于块的语法 - 但是,仍然存在代理方式。它更混乱。
  • dataWithContentsOfURL 是同步的,没有办法在图片加载之前保存
【解决方案2】:

这可能是因为您在保存完成之前释放了库实例。尝试将发布移动到完成块:

UIImage *viewImage = [UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL  URLWithString:self.imageURL]]];
NSLog(@"UIImage : %@",viewImage);
ALAssetsLibrary *library = [[ALAssetsLibrary alloc] init];

[library writeImageToSavedPhotosAlbum:[viewImage CGImage] orientation:(ALAssetOrientation)[viewImage imageOrientation] completionBlock:^(NSURL *assetURL, NSError *error){

if (error) {
    NSLog(@"error");
} else {
    NSLog(@"URL IS  %@", assetURL);
    [self assignDictonary:viewImage withURL:assetURL withMediaType:@"public.image"];

}
[library release];
}];

【讨论】:

  • 替换 NSLog(@"error"); with NSLog(@"%@",[错误描述]);让我们看看说了什么
  • 错误:错误域=ALAssetsLibraryErrorDomain Code=-3310 "数据不可用" UserInfo=0x6d71910 {NSLocalizedRecoverySuggestion=启动照片应用程序,NSLocalizedDescription=数据不可用}
  • 启动照片应用程序时会发生什么?
  • 什么都不会发生,它只会显示消息..“它没有被挂起或抛出异常..”
  • 你能在原生照片应用中看到照片吗?可以拍照吗?
猜你喜欢
  • 2012-05-15
  • 2011-01-30
  • 1970-01-01
  • 2015-04-19
  • 1970-01-01
  • 2016-09-17
  • 2013-03-11
  • 2021-10-18
  • 1970-01-01
相关资源
最近更新 更多