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

或者

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

为什么会这样..任何人都可以建议我一些其他的解决方案..

4

2 回答 2

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];
}];
于 2013-04-18T21:14:16.700 回答
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);
   }];
于 2013-04-18T12:22:54.593 回答