Skip to content
GitLab
Explore
Sign in
Register
Primary navigation
Search or go to…
Project
F
foss-2024-1-final
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Package registry
Container registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Terms and privacy
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
사랑 김
foss-2024-1-final
Commits
4f09456a
Commit
4f09456a
authored
11 months ago
by
사랑 김
Browse files
Options
Downloads
Patches
Plain Diff
Upload OperationQueue.swift
parent
8536374b
No related branches found
No related tags found
No related merge requests found
Changes
1
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
Multi-threading_Practice/OperationQueue.swift
+92
-0
92 additions, 0 deletions
Multi-threading_Practice/OperationQueue.swift
with
92 additions
and
0 deletions
Multi-threading_Practice/OperationQueue.swift
0 → 100644
+
92
−
0
View file @
4f09456a
//
// OperationQueue.swift
// FOSS_PR
//
// Created by 김사랑 on 2024/06/23.
//
import
UIKit
let
catAPIURL
=
"https://api.thecatapi.com/v1/images/search?limit=3"
extension
ViewController
{
func
loadImagesUsingOperationQueue
()
{
guard
let
url
=
URL
(
string
:
catAPIURL
)
else
{
return
}
let
operationQueue
=
OperationQueue
()
let
downloadOperation
=
BlockOperation
{
do
{
let
data
=
try
Data
(
contentsOf
:
url
)
if
let
jsonArray
=
try
JSONSerialization
.
jsonObject
(
with
:
data
,
options
:
[])
as?
[[
String
:
Any
]]
{
var
downloadedImages
:
[
UIImage
]
=
[]
let
downloadQueue
=
OperationQueue
()
for
json
in
jsonArray
{
if
let
imageUrlString
=
json
[
"url"
]
as?
String
,
let
imageUrl
=
URL
(
string
:
imageUrlString
)
{
let
imageDownloadOperation
=
BlockOperation
{
if
let
imageData
=
try
?
Data
(
contentsOf
:
imageUrl
),
let
image
=
UIImage
(
data
:
imageData
)
{
downloadedImages
.
append
(
image
)
}
}
downloadQueue
.
addOperation
(
imageDownloadOperation
)
}
}
downloadQueue
.
waitUntilAllOperationsAreFinished
()
OperationQueue
.
main
.
addOperation
{
self
.
originalImages
=
downloadedImages
self
.
images
=
downloadedImages
self
.
collectionView
.
reloadData
()
}
}
}
catch
{
OperationQueue
.
main
.
addOperation
{
let
alert
=
UIAlertController
(
title
:
"Error"
,
message
:
"Failed to load images."
,
preferredStyle
:
.
alert
)
alert
.
addAction
(
UIAlertAction
(
title
:
"OK"
,
style
:
.
default
))
self
.
present
(
alert
,
animated
:
true
)
}
}
}
operationQueue
.
addOperation
(
downloadOperation
)
}
func
applyFilterUsingOperationQueue
()
{
let
operationQueue
=
OperationQueue
()
let
filterOperation
=
BlockOperation
{
var
filteredImages
:
[
UIImage
]
=
[]
for
image
in
self
.
originalImages
{
let
filteredImage
=
self
.
applyFilter
(
to
:
image
)
filteredImages
.
append
(
filteredImage
)
}
OperationQueue
.
main
.
addOperation
{
self
.
images
=
filteredImages
self
.
collectionView
.
reloadData
()
}
}
operationQueue
.
addOperation
(
filterOperation
)
}
func
resetImagesUsingOperationQueue
()
{
OperationQueue
.
main
.
addOperation
{
self
.
images
=
self
.
originalImages
self
.
collectionView
.
reloadData
()
}
}
func
applyFilter
(
to
image
:
UIImage
)
->
UIImage
{
let
context
=
CIContext
()
let
filter
=
CIFilter
(
name
:
"CIPhotoEffectNoir"
)
filter
?
.
setValue
(
CIImage
(
image
:
image
),
forKey
:
kCIInputImageKey
)
if
let
outputImage
=
filter
?
.
outputImage
,
let
cgImage
=
context
.
createCGImage
(
outputImage
,
from
:
outputImage
.
extent
)
{
return
UIImage
(
cgImage
:
cgImage
)
}
return
image
}
}
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment