How to access Asset Catalog (images) after downloaded through NSBundleResourceRequest? - on-demand-resources

I cannot access images inside an asset catalog after it's been downloaded through NSBundleResourceRequest (on demand resource).
My code, say the image set name is "snow_4" with on demand resource tag "tag1"
NSBundleResourceRequest* resourceRequest = [[NSBundleResourceRequest alloc] initWithTags:[NSSet setWithObjects:#"tag1", nil]];
[resourceRequest conditionallyBeginAccessingResourcesWithCompletionHandler:
^(BOOL resourcesAvailable){
// if resource is not available download it
if (!resourcesAvailable) {
[resourceRequest beginAccessingResourcesWithCompletionHandler:
^(NSError * __nullable error){
if(!error){
UIImage* image = [UIImage imageNamed:#"snow_4"]; // image is null
}
}];
}else{
UIImage* image = [UIImage imageNamed:#"snow_4"]; // image is null
}
}];
Below is my Disk Report, please note that [UIImage imageNamed:#"snow_3"] (marked as 'In Use') returns a correct object but not images that are marked as 'Downloaded'
Appreciating your time and help!
Thanks,
Mars

Make sure NSBundleResourceRequest is not released early. Try to let NSBundleResourceRequest variable globally,
and access the image with UIImage(named: "xxx", in: self.bundleReq.bundle, compatibleWith: nil)

do this
image = [UIImage imageNamaed:#"snow_4" inBundle:[resourceRequest bundle]];

let tags = NSSet(array: ["tag1","tag2"])
let resourceRequest = NSBundleResourceRequest(tags: tags as! Set<String>)
resourceRequest.conditionallyBeginAccessingResourcesWithCompletionHandler {(resourcesAvailable: Bool) -> Void in
if resourcesAvailable {
// Do something with the resources
} else {
resourceRequest.beginAccessingResourcesWithCompletionHandler {(err: NSError?) -> Void in
if let error = err {
print("Error: \(error)")
} else {
// Do something with the resources
}
}
}
}

Related

Is there any function to extract all the frames of a video that is downloaded and picked from Documents Directory in iOS Swift?

I am trying to extract all the frames from a video. Code is working fine for the video in Bundle Path, but it fails when i give a link of the video or I try to pick the video from File Manager.
Following are the issues that i have encountered:
AVAsset throws an error "Error Domn=AVFoundationErrorDomain Code=-11838 "Cannot initialize an instance of AVAssetReader with an asset at non-local URL".
When video is picked from Documents Directory, the fetched asset tracks are always 0.
Following is the code that i am using right now.
let asset = AVAsset(url: URL(string: path!)!)
self.playerController.player = AVPlayer(playerItem: AVPlayerItem(asset: asset))
if let path = path {
let asset:AVAsset = AVAsset(url: URL(string: path)!)
self.playerController.player = AVPlayer(playerItem: AVPlayerItem(asset: asset))
self.player.play()
asset.loadTracks(withMediaType: .video) { fetchedTracks, err in
do{
let reader = try AVAssetReader(asset: asset)
// read video frames as BGRA
let trackReaderOutput = AVAssetReaderTrackOutput(track: (fetchedTracks?.first)!, outputSettings:[String(kCVPixelBufferPixelFormatTypeKey): NSNumber(value: kCVPixelFormatType_32BGRA)])
reader.add(trackReaderOutput)
reader.startReading()
while let sampleBuffer = trackReaderOutput.copyNextSampleBuffer() {
print("sample at time \(CMSampleBufferGetPresentationTimeStamp(sampleBuffer))")
if CMSampleBufferGetImageBuffer(sampleBuffer) != nil {
// process each CVPixelBufferRef here
guard let imageBuffer: CVPixelBuffer = CMSampleBufferGetImageBuffer(sampleBuffer) else { return }
let ciimage = CIImage(cvPixelBuffer: imageBuffer)
self.frames.append(UIImage(ciImage: ciimage))
// see CVPixelBufferGetWidth, CVPixelBufferLockBaseAddress, CVPixelBufferGetBaseAddress, etc
}
}
self.collectionViewFrames.reloadData()
} catch let err {
print(err)
}
}
}

flutter with firebase - detect the url string whether it is image(jpg) or video(avi)

I'm beginner with these Google's products and got 'serious' problem.
I uploaded photos and videos to firebase storage and urls of the photos and videos in the firebase storage is generated and stored automatically in firebase database. In my flutter lib, I could call those urls by my own code and display the url's image on the avd screen.
Image.network(url) is the code to display image url from firebase. But I also wanna display video url's asset simultaneously with single code. That is, videos and photos should be in single screen! In this case, Image.network(url) doesn't work anymore..
If I change that image.network code for video format according to video_player plug-in, I cannot display image asset anymore and if I stay same with that Image.network(url) code, I cannot display video url from firebase. So here is the question:
How can I detect whether the firebase's url string is image or video with my flutter code, and display that asset on the 'single screen' whatever the file format is(at least video and photo) with integrated flutter code?
url example
It's not much of a deal since type of the media is in the URL. You can parse it as a
Uri object then extract the type.
import 'dart:core';
enum UrlType { IMAGE, VIDEO, UNKNOWN }
void main() async {
var imageUrl =
'https://firebasestorage.googleapis.com/v0/b/myAppCodeNameForFirebase.appspot.com/o/Posts%20Pictures%2Fiufri095620200814.jpg?alt=media&token=89b6c22f-b8dd-4cff-9395-f53fc0808824';
var videoUrl =
'https://firebasestorage.googleapis.com/v0/b/myAppCodeNameForFirebase.appspot.com/o/Posts%20Pictures%2Fiufri095620200814.mp4?alt=media&token=89b6c22f-b8dd-4cff-9395-f53fc0808824';
print(getUrlType(imageUrl));
print(getUrlType(videoUrl));
}
UrlType getUrlType(String url) {
Uri uri = Uri.parse(url);
String typeString = uri.path.substring(uri.path.length - 3).toLowerCase();
if (typeString == "jpg") {
return UrlType.IMAGE;
}
if (typeString == "mp4") {
return UrlType.VIDEO;
} else {
return UrlType.UNKNOWN;
}
}
Let me give you an idea.
Consider implementing this scenario.
var url = 'domain.com/file.jpg?querySegment';
In your widget area,
child: url.contains('.mp4?') ? VideoWidget() : ImageWidget()
also, even with multiple conditions,
child: (url.contains('.jpg?') || url.contains('.png?')) ? ImageWidget() : VideoWidget()
May this suits your case.
Improving upon the accepted answer, here is what I have in my code. I needed a way to identify many types of videos and images from the passed url.
Using path package to idetify the extension
Have a list of video and image extension
import 'package:path/path.dart' as p;
enum UrlType { IMAGE, VIDEO, UNKNOWN }
class UrlTypeHelper {
static List<String> _image_types = [
'jpg',
'jpeg',
'jfif',
'pjpeg',
'pjp',
'png',
'svg',
'gif',
'apng',
'webp',
'avif'
];
static List<String> _video_types = [
"3g2",
"3gp",
"aaf",
"asf",
"avchd",
"avi",
"drc",
"flv",
"m2v",
"m3u8",
"m4p",
"m4v",
"mkv",
"mng",
"mov",
"mp2",
"mp4",
"mpe",
"mpeg",
"mpg",
"mpv",
"mxf",
"nsv",
"ogg",
"ogv",
"qt",
"rm",
"rmvb",
"roq",
"svi",
"vob",
"webm",
"wmv",
"yuv"
];
static UrlType getType(url) {
try {
Uri uri = Uri.parse(url);
String extension = p.extension(uri.path).toLowerCase();
if (extension.isEmpty) {
return UrlType.UNKNOWN;
}
extension = extension.split('.').last;
if (_image_types.contains(extension)) {
return UrlType.IMAGE;
} else if (_video_types.contains(extension)) {
return UrlType.VIDEO;
}
} catch (e) {
return UrlType.UNKNOWN;
}
return UrlType.UNKNOWN;
}
}
/// Usage
if(UrlTypeHelper.getType(message) == UrlType.IMAGE) {
/// handle image
}
else if(UrlTypeHelper.getType(message) == UrlType.VIDEO) {
/// handle video
}

Load Image in async and update table cell in main thread

I get the following error when I try to load images from URL in async and render them in the main thread.The app is build with Xcode9-beta.
UIImageView.image must be used from main thread only
The images update only if I press on a cell in the tableView(and only for that particular button):
func updateUI(partyRock: PartyRock) {
videoTitle.text = partyRock.videoTitle
let url = URL(string: partyRock.imageURL)!
DispatchQueue.global().async {
do {
let data = try Data(contentsOf: url)
DispatchQueue.global().sync {
self.videoPreviewImage.image = UIImage(data: data)
}
} catch {
// handle error
}
}
}

How can I tell if the image returned from didFinishPickingMediaWithInfo was from the camera or the photoalbum?

I have a view controller that needs to be able to choose a picture from the photo album and also from the camera. I can only have 1 delegate method for didFinishPickingMediaWithInfo and while I can tell if it's an image, I can't seem to tell if it's from the album or from the camera (and I need to save it in the album first). Is there anything in the info that can help me distinguish from the two?
Thanks...
Because the UIImagePickerController is passed to the method, all you have to do is:
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {
if ([picker sourceType] == UIImagePickerControllerSourceTypeCamera) {
// Do something with an image from the camera
} else {
// Do something with an image from another source
}
}
In Swift3:
func imagePickerController(picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [NSObject : AnyObject]) {
if picker.sourceType == .camera {
// Do something with an image from the camera
}
else {
// Do something with an image from another source
}
}

Display an ashx image using jQuery?

I've been trying to use the jQuery plugin Colorbox to display images I have in my DB through an ashx file. Unfortunately it just spits a bunch of gibberish at the top of the page and no image. Can this be done? Here is what I have so far:
$(document).ready
(
function ()
{
$("a[rel='cbImg']").colorbox();
}
);
...
<a rel="cbImg" href="HuntImage.ashx?id=15">Click to see image</a>
UPDATE:
My ashx file is writing the binary out:
context.Response.ContentType = "image/bmp";
context.Response.BinaryWrite(ba);
Colorbox has an option 'photo'. If you set this to true in your constructor then it will force it to render the photo.
$(target).colorbox({photo: true});
You should be setting the src attribute in the client side.
<img src="HuntImage.ashx?id=15" ..../>
The handler
public class ImageRequestHandler: IHttpHandler, IRequiresSessionState
{
public void ProcessRequest(HttpContext context)
{
context.Response.Clear();
if(context.Request.QueryString.Count != 0)
{
//Get the stored image and write in the response.
var storedImage = context.Session[_Default.STORED_IMAGE] as byte[];
if (storedImage != null)
{
Image image = GetImage(storedImage);
if (image != null)
{
context.Response.ContentType = "image/jpeg";
image.Save(context.Response.OutputStream, ImageFormat.Jpeg);
}
}
}
}
private Image GetImage(byte[] storedImage)
{
var stream = new MemoryStream(storedImage);
return Image.FromStream(stream);
}
public bool IsReusable
{
get { return false; }
}
}
It appears that I can't do what I am trying using colorbox with an ashx image. If anyone finds a way please post it here.
I considered deleting the question but I will leave it up incase someone else runs into the same issue.
Find this function around line 124 (colorbox 1.3.15)
// Checks an href to see if it is a photo.
// There is a force photo option (photo: true) for hrefs that cannot be matched by this regex.
function isImage(url) {
return settings.photo || /\.(gif|png|jpg|jpeg|bmp)(?:\?([^#]*))?(?:#(\.*))?$/i.test(url);
}
On line 127, add |ashx after bmp in (gif|png|jpg|jpeg|bmp) so it reads like this:
// Checks an href to see if it is a photo.
// There is a force photo option (photo: true) for hrefs that cannot be matched by this regex.
function isImage(url) {
return settings.photo || /\.(gif|png|jpg|jpeg|bmp|ashx)(?:\?([^#]*))?(?:#(\.*))?$/i.test(url);
}
This is working just fine for me in Sitecore 6.2 :)

Resources