티스토리 뷰

반응형

플러터에서 갤러리나 카메라로부터 찍힌 사진을 가져와서 원하는 갯수로 분할하고 분할된 사진들을 각각 저장 그리고 sns에 공유하는 기능을 구현해 보았습니다. 생각보다 까다로웠습니다. 

아래는 기능구현 시험영상입니다. 

 

 

 

1. 사진 불러오기

먼저 사진을 불러오기 위해서 image_picker라는 패키지를 사용합니다. 여기서 갤러리에서 불러오거나 아니면 카메라로 사진을 찍은 후 그 사진을 바로 업로드 할 수 있습니다. 

Flutter plugin for selecting images from the Android and iOS image library, and taking new pictures with the camera.

 

https://pub.dev/packages/image_picker

 

image_picker | Flutter Package

Flutter plugin for selecting images from the Android and iOS image library, and taking new pictures with the camera.

pub.dev

<IOS>

Add the following keys to your Info.plist file, located in <project root>/ios/Runner/Info.plist:

  • NSPhotoLibraryUsageDescription - describe why your app needs permission for the photo library. This is called Privacy - Photo Library Usage Description in the visual editor.
  • NSCameraUsageDescription - describe why your app needs access to the camera. This is called Privacy - Camera Usage Description in the visual editor.
  • NSMicrophoneUsageDescription - describe why your app needs access to the microphone, if you intend to record videos. This is called Privacy - Microphone Usage Description in the visual editor

<Android>

 

2. 사진을 정사각형으로 만들기

불러온 사진을 GFAvatarShape.square property를 사용하여 정사각형으로 만들어 줍니다. 

 

3. 사진을 분할하기 

 

아래의 패키지를 사용하여, 사진의 데이터 타입을 바꾸어 주고 또한 분할을 합니다. 

 

https://pub.dev/packages/image

 

image | Dart Package

Provides server and web apps the ability to load, manipulate, and save images with various image file formats including PNG, JPEG, GIF, BMP, WebP, TIFF, TGA, PSD, PVR, and OpenEXR.

pub.dev

 

 

참고 자료들:

 

https://stackoverflow.com/questions/58363167/how-to-split-divide-image-in-parts-in-flutter

 

How to split/divide image in parts in Flutter

How to split an image into equal-sized parts? Just taking an image from asset and splitting it into equal parts in a grid-like manner, so that each image part can be used as a separate image. Some...

stackoverflow.com

https://stackoverflow.com/questions/44665720/how-do-i-crop-an-image-in-flutter

 

How do I crop an image in Flutter?

Let's say I have a rectangular, portrait image: I'd like to crop it, such that it's rendered like this: How can I do this in Flutter? (I don't need to resize the image.) (Image from https://fli...

stackoverflow.com

 

https://stackoverflow.com/questions/57601908/can-we-crop-an-image-with-point-and-size-in-flutter

 

Can we crop an image with point and size in Flutter?

I'm writing a face detect app and I want to crop the face that have detected on screen with the bounding box of that face. I have searched all the way but only can crop with width height and aspect

stackoverflow.com

4. 불러온 사진 그리고 분할된 사진을 저장하기

 

gallery_saver라는 아래의 패키지를 이용합니다. 

 

https://pub.dev/packages/gallery_saver

 

gallery_saver | Flutter Package

Saves images and videos from network or temporary file to external storage. Both images and videos will be visible in Android Gallery and iOS Photos.

pub.dev

아래는 코드구현입니다. 

 

 

5. 사진을 공유하기

 

사진 sns 공유는 share_plus라는 패키지를 사용합니다. 

https://pub.dev/packages/share_plus

 

share_plus | Flutter Package

Flutter plugin for sharing content via the platform share UI, using the ACTION_SEND intent on Android and UIActivityViewController on iOS.

pub.dev

 

아래는 코드구현입니다. 

6. 분할된 사진 저장

 

분할된 사진을 갤러리에 저장하는 로직은 이렇습니다

1. 사진을 분할하는 함수를 불러서 List<image> a 라는 변수에 저장을 하고, 

for loop 함수안에서 분할된 사진 index 갯수만큼 스크린샷을 찍고 그것을 저장합니다. 

 

아래는 코드 구현입니다. 

 

반응형
반응형
공지사항
최근에 올라온 글
최근에 달린 댓글
Total
Today
Yesterday
링크
TAG more
«   2025/02   »
1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28
글 보관함