NSOperation mainQueue issue

独自空忆成欢 提交于 2019-12-11 05:48:28

问题


I have slideshow, and I want to show Big images, I added to [NSOperation mainQueue] operation with low priority, this operation shows the image.

If image is small , everything is OK, but when image is about 5Mb, the view freeze for 1 second, and I can't scroll my slideshow. I think, that displaying big images just so difficult for iPhone, that main queue is too overloaded.

But I don't inderstand it , because all my displaying code is executed in low priority operation.

Here is the displaying code.

[imageView removeFromSuperview];
imageView = nil;

// reset our zoomScale to 1.0 before doing any further calculations
self.zoomScale = 1.0;

// make a new UIImageView for the new image
self.imageView = [[[UIImageView alloc] initWithImage:image] autorelease];
[self addSubview:imageView];

self.contentSize = [image size];
[self setMaxMinZoomScalesForCurrentBounds];
self.zoomScale = self.minimumZoomScale;

May be I can set the priority for gesture recognizers (the regular questure recognizers for UIScrollView?)

Update

Please look at my new topic, I described the issue more properly my topik


回答1:


Priority has to do with scheduling. If you queue up a bunch of operations during a runloop iteration then they will be executed by their priority on that queue.

One solution to speed this up would be to either include resources that are scaled to the exact size that you are displaying them in. If you are trying to show a 2000x2000 px image in a 200x200 area then the system to scale all this stuff in memory. You can also dynamically create smaller to fit images programmatically. This can be done on a background queue so your UI is still responsive.

How to resize the image programmatically in objective-c in iphone




回答2:


If I understand you correclty and you have done something like

NSOperationQueue* queue = [NSOperationQueue mainQueue];

Then any NSOperation you add to it will execute on the main dispatch queue which is concurrent (and responsible for executing tasks on the main thread). That would explain the freeze. You can create your own queue which would start a thread for every NSOperation and would free the main thread to render the UI normally as:

NSOperationQueue* queue = [[NSOperationQueue alloc] init];
[queue addOperation:operation];

This however would cause a problem. When the image is finished loading and you pass it to an UIImageView on the screen there will be a big delay until the Image is actually rendered because the main (UI) thread will not be aware of the action until it chooses to refresh (a few seconds later). The solution to this is to add a 'performInMainThread' message to the end of the main method of the NSOperation as such:

-(void)main {
    NSData *bgImageData = [NSData dataWithContentsOfURL:self.targetUrl];
    UIImage *img = [UIImage imageWithData:bgImageData];
    [self performSelectorOnMainThread:@selector(insertImageLoaded:)
                           withObject:img
                        waitUntilDone:YES];
}

The 'insertImageLoaded' is in the NSOperation and would load call a setImage:(UIImage*)img to the component you want.



来源:https://stackoverflow.com/questions/11158294/nsoperation-mainqueue-issue

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!