图像未在iOS 4.3中保存(下载)

时间:2013-04-18 11:48:11

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

我正在使用

     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];

OR

 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 ...

为什么会如此......任何人都可以向我提出其他解决方案......

2 个答案:

答案 0 :(得分:0)

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

另一种方法是使用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);
   }];

答案 1 :(得分:0)

这可能是因为您在保存完成之前释放了库实例。尝试将版本移至完成块:

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];
}];