Reputation: 370
I'm creating a tableView
which contains the images from gallery picked by the user, then I take the selected imagePath
to place it on an UIImageView
.
To set an iOS gallery image in a UIImageView
,
I'm using:
NSString *imgPath = @"/var/mobile/Media/DCIM/105APPLE/IMG_5903.JPG";//Hardcoded path just for test, image actually exists on iOS device
self.imgViewContainer.image = [UIImage imageWithContentsOfFile: imgPath];
This code which works on Simulator.
I tested on a real device and I noticed that the UIImageView
is empty after execute the above code.
Is there a way to achieve this using a real device?
Upvotes: 2
Views: 768
Reputation: 42449
Due to app sandboxing, your app does not have permission to view the contents of /var/mobile/Media
(or really any subdirectory of /var/
). Consider the code below:
do {
let fileList = try FileManager.default.contentsOfDirectory(atPath: "/var/mobile/Media/DCIM/")
} catch {
print(error)
}
contentsOfDirectory(atPath:)
with throw an error with these details:
Error Domain=NSCocoaErrorDomain Code=257 "The file “DCIM” couldn’t be opened because you don’t have permission to view it." UserInfo={NSFilePath=/var/mobile/Media/DCIM/, NSUserStringVariant=( Folder ), NSUnderlyingError=0x170045700 {Error Domain=NSPOSIXErrorDomain Code=1 "Operation not permitted"}}
This example is in Swift, but the equivalent Objective-C code would have the same error.
There is no way around this (unless your are using a jailbroken device running your application as the root user). Use UIImagePickerController
to ask the user for an image.
Upvotes: 6