簡體   English   中英

如何將圖像URL保存到照片庫,然后使用保存的圖像

[英]How to save image URL to photo library and subsequently use the saved image

我正在為我的應用實施“在線圖片搜索”功能。 我需要的流程如下:

1)檢索用戶想要使用的圖像URL

2)將圖像(通過URL)保存到手機的相冊中

3)通過圖像選擇器控制器檢索保存的圖像,並打開移動和縮放屏幕

4)使用從相冊中檢索的圖像。

任何人都可以建議我在獲得圖像URL后如何執行上述步驟?

您可以使用此代碼將圖像保存在相冊中

    UIImageWriteToSavedPhotosAlbum(yourImage, self, @selector(image:didFinishSavingWithError:contextInfo:), nil);



- (void)image:(UIImage *)image didFinishSavingWithError:(NSError *)error contextInfo:(void *)contextInfo
{
    if (error != NULL)
    {
        // handle error
    }
    else 
    {
        // handle ok status
    }
}

現在,為了在另一個線程中執行代碼,我會編寫這樣的代碼

// load data in new thread
[NSThread detachNewThreadSelector:@selector(downloadImage) toTarget:self withObject:nil];

您可以在代碼,按鈕或任何其他UIKit控件中的任何位置使用此方法。 那么你將需要做出艱苦工作的方法。

- (void)downloadImage
{

    // network animation on
    [UIApplication sharedApplication].networkActivityIndicatorVisible = YES;

    // create autorelease pool
    NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init]; 

   // save image from the web
    UIImageWriteToSavedPhotosAlbum([UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:@"your_image_address.com"]]], self, @selector(image:didFinishSavingWithError:contextInfo:), nil);

    [self performSelectorOnMainThread:@selector(imageDownloaded) withObject:nil waitUntilDone:NO ];  

    [pool drain];       

}

- (void)imageDownloaded
{

    // network animation off
    [UIApplication sharedApplication].networkActivityIndicatorVisible = NO;

    // do whatever you need to do after 
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM