1. 程式人生 > >iOS 儲存圖片到相簿, 儲存到指定的路徑

iOS 儲存圖片到相簿, 儲存到指定的路徑

程式碼很簡單隻有一句話

不多說上程式碼:

<span style="font-size:24px;">UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
    
    // 儲存圖片到指定的路徑
    NSData *data = UIImagePNGRepresentation(newImage);
    [data writeToFile:@"/Users/userName/Desktop/myShaoNv.png" atomically:YES];
    </span>

<span style="font-size:24px;"> // 儲存image 到相簿中
    UIImageWriteToSavedPhotosAlbum(newImage, self, @selector(imageSavedToPhotosAlbum:didFinishSavingWithError:contextInfo:), nil);
    

// 儲存到相簿的代理方法, 判斷儲存的結果是否成功還是失敗的資訊
// 實現imageSavedToPhotosAlbum:didFinishSavingWithError:contextInfo:
- (void)imageSavedToPhotosAlbum:(UIImage *)image didFinishSavingWithError:(NSError *)error contextInfo:(void *)contextInfo
{
    NSString *message = @"儲存照片";
    if (!error) {
        message = @"儲存成功";
        
    } else
    {
        message = [error description];
    }
    NSLog(@"message is %@", message);
}
</span>